for_string.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // for_string.go
  2. package main
  3. import "fmt"
  4. func main() {
  5. str := "Go is a beautiful language!"
  6. fmt.Printf("The length of str is: %d\n", len(str))
  7. for ix :=0; ix < len(str); ix++ {
  8. fmt.Printf("Character on position %d is: %c \n", ix, str[ix])
  9. }
  10. str2 := "日本語"
  11. fmt.Printf("The length of str2 is: %d\n", len(str2))
  12. for ix :=0; ix < len(str2); ix++ {
  13. fmt.Printf("Character on position %d is: %c \n", ix, str2[ix])
  14. }
  15. }
  16. /* Output:
  17. The length of str is: 27
  18. Character on position 0 is: G
  19. Character on position 1 is: o
  20. Character on position 2 is:
  21. Character on position 3 is: i
  22. Character on position 4 is: s
  23. Character on position 5 is:
  24. Character on position 6 is: a
  25. Character on position 7 is:
  26. Character on position 8 is: b
  27. Character on position 9 is: e
  28. Character on position 10 is: a
  29. Character on position 11 is: u
  30. Character on position 12 is: t
  31. Character on position 13 is: i
  32. Character on position 14 is: f
  33. Character on position 15 is: u
  34. Character on position 16 is: l
  35. Character on position 17 is:
  36. Character on position 18 is: l
  37. Character on position 19 is: a
  38. Character on position 20 is: n
  39. Character on position 21 is: g
  40. Character on position 22 is: u
  41. Character on position 23 is: a
  42. Character on position 24 is: g
  43. Character on position 25 is: e
  44. Character on position 26 is: !
  45. The length of str2 is: 9
  46. Character on position 0 is: æ
  47. Character on position 1 is: —
  48. Character on position 2 is: ¥
  49. Character on position 3 is: æ
  50. Character on position 4 is: œ
  51. Character on position 5 is: ¬
  52. Character on position 6 is: è
  53. Character on position 7 is: ª
  54. Character on position 8 is: ž
  55. */