This is the 15th chapter of Golang Language Learning Tutorial
What is a string
The string in the Go language is a byte slice. You can create a string by placing the contents between double quotes.
package mainimport "fmt"func main() { name := "hello world" fmt.Println(name)}
Program output:hello world
The string in Go is Unicode-encoded and encoded using UTF-8.
Get each byte of a string separately
Because the string is a byte slice, you can get every byte of a string.
package mainimport "fmt"func printbytes(s string) { //定义函数 for i := 0; i < len(s); i++ { //len(s) 返回字符串中字符的数量 fmt.Printf( "%x ", s[i]) //%x 指定打印字符串的16进制编码 }}func printchars(s string) { for i := 0; i< len(s); i++ { fmt.Printf("%c ", s[i]) //%c 指定打印字符串的字符 }}func main() { name := "hello world" printbytes(name) fmt.Println("\n") printchars(name)}
In the above program, Len (s) is used to return the number of characters in a string,%x specifies the 16 encoding of the printed string, and%c is used to specify the number of printed strings.
The above program output is:
6c 6c 6f, 6f, 6c 64
H e l l o w o R l D
The above program gets each character of the string, although it appears to be legal, but there is a serious bug. Let me break this code down to see what we did wrong.
package mainimport "fmt"func printbytes(s string) { //定义函数 for i := 0; i < len(s); i++ { //len(s) 返回字符串中字符的数量 fmt.Printf( "%x ", s[i]) //%x 指定打印字符串的16进制编码 }}func printchars(s string) { for i := 0; i< len(s); i++ { fmt.Printf("%c ", s[i]) //%c 指定打印字符串的字符 }}func main() { name := "hello world" printbytes(name) fmt.Println("\n") printchars(name) name = "Señor" fmt.Println("\n") printchars(name)}
In the above program, we try to output Señor characters, but output the wrong S eã±o R. Why did the program split up Hello World perfectly, but Señor the segmentation was wrong? This is because ñ the Unicode code point is U+00F1 . Its UTF-8 encoding takes up two bytes of C3 and B1. Its UTF-8 encoding takes up two bytes of C3 and B1. When we print the characters, it is wrong to assume that each character's encoding will only occupy one byte. In UTF-8 encoding, a code point may occupy more than one byte of space. So what do we do? Rune can help us solve this problem.
Rune
Rune is the built-in type of Go language, and it is also a nickname for Int32. In the Go language, Rune represents a code point. Code points can be represented by a rune, regardless of the number of bytes consumed. Let's change the program above and use Rune to print characters.
package mainimport "fmt"func printbytes(s string) { //定义函数 for i := 0; i < len(s); i++ { //len(s) 返回字符串中字符的数量 fmt.Printf( "%x ", s[i]) //%x 指定打印字符串的16进制编码 }}func printchars(s string) { runes := []rune(s) //字符串被转化为一个 rune 切片 for i := 0; i< len(s); i++ { fmt.Printf("%c ", runes[i]) //%c 指定打印字符串的字符 }}func main() { name := "hello world" printbytes(name) fmt.Println("\n") printchars(name) name = "Señor" fmt.Println("\n") printchars(name)}
In the above program, the string is converted into a rune slice.
The program output is:
6c 6c 6f, 6f, 6c 64
H e l l o w o R l D
S Eño R
The string is immutable
The string in go is immutable and cannot be changed once created:
package mainimport ( "fmt")func mutate(s string)string { s[0] = 'a'//any valid unicode character within single quote is a rune return s}func main() { h := "hello" fmt.Println(mutate(h))}
In the above program want to change the first character of H into a, but error: main.go:8: cannot assign to s[0] , it can be seen that the string is not allowed to modify.
To modify a string, you can convert the string into a rune slice. The slice can then make any desired changes and then convert to a string.
package mainimport ( "fmt")func mutate(s []rune) string { s[0] = 'a' return string(s)}func main() { h := "hello" fmt.Println(mutate([]rune(h)))}
In the above program, the function mutate receives an incoming rune slice, changes the first character to a, and then converts it to a string output. So the above program output is:aello
Above for learning Golang String Chapter