String


In Go, string is an immutable array of bytes. So if created, we can’t change its value. E.g.:

  1. package main
  2. func main() {
  3. s := "Hello"
  4. s[0] = 'h'
  5. }

The compiler will complain:

  1. cannot assign to s[0]

To modify the content of a string, you could convert it to a byte array. But in fact, you do not operate on the original string, just a copy:

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := "Hello"
  5. b := []byte(s)
  6. b[0] = 'h'
  7. fmt.Printf("%s\n", b)
  8. }

The result is like this:

  1. hello

Since Go uses UTF-8 encoding, you must remember the len function will return the string’s byte number, not character number:

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := "日志log"
  5. fmt.Println(len(s))
  6. }

The result is:

  1. 9

Because each Chinese character occupied 3 bytes, s in the above example contains 5 characters and 9 bytes.

If you want to access every character, for ... range loop can give a help:

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := "日志log"
  5. for index, runeValue := range s {
  6. fmt.Printf("%#U starts at byte position %d\n", runeValue, index)
  7. }
  8. }

The result is:

  1. U+65E5 '日' starts at byte position 0
  2. U+5FD7 '志' starts at byte position 3
  3. U+006C 'l' starts at byte position 6
  4. U+006F 'o' starts at byte position 7
  5. U+0067 'g' starts at byte position 8

Reference:
Strings, bytes, runes and characters in Go;
The Go Programming Language.