First, the principle of string analysis
1. The bottom of the string is a byte array, so can and []byte types are converted to each other;(strings can be stored in text or binary, because they are inherently a byte stream)
2. The characters in the string can not be modified, the string is a read-only type, can not be directly modified, how to modify it (to be solved (Exercise 1 is resolved (conversion to byte array to modify))?
Examples are as follows:
We can find the characters in the string that have gone red and have an English hint that cannot be modified.
Indicates that the string in the go language is a read-only type and cannot be modified directly.
3. the string is made up of byte bytes, so the length of the string is the length of the byte , and Byte is a int8 alias ;
Example See Example 1-1
4. The rune type is used to represent UTF8 characters, and a rune character consists of 1 or more bytes , and Rune is an alias of Int32 ;
Add:
1) character-to-byte relationship?
A: If it is an English character, 1 characters corresponds to 1 byte (bytes), if it is Chinese characters, 1 characters correspond to 2, even 3, 4 byte;
2) UTF8 code?
A: UTF8 for the biggest advantage is that, in fact, the variable, if the English character, it gives 1 byte, if it is Chinese according to its needs given the number of specified characters, which for the network bandwidth savings is very important.
3) Rune Type is actually int32 type;
Example 1-1
Declares that the Array.go file belongs to the main package main//introduces other package FMT is the system's standard input Output library import ("FMT")//Ingress execution function, the main package must have a main function, which is mandatory in the Go language func Main () {//Declare a str variable of type string var str string//Method two str: = "string" str = "ABC man"//Declare a byte array can also be called a byte array is a meaning var b [ ]byte = []byte (str) ///Method two B: =[]byte (str) //cast str to byte array ([]byte) var chars []rune = []rune (str)/// Converts STR to the rune array ([]rune)//formats the value of print B and the length of the B-byte array in FMT. Printf ("b =%v,len (str) =%d\n", B, Len (str)) FMT. Printf ("%c\n", "the") fmt. Printf ("Chars=%v,chars count:%d\n", chars, Len (chars))}
The results of the implementation are as follows:
D:\project\src\base-3\array-1>go Run Array.gob =[97 98 (177 137 229 173 144],len (str) =9achars=[97 98 99 27721 233 76],chars Count:5
Explain:
1) The output is ASCII code (characters, whether English or Chinese in the underlying storage is an integer ASCII code), 97 corresponds to A;
2) from the instance to see a "Han" word consists of three bytes, the corresponding ASCII code is 230 177 137;
3) The length of the STR string is 9, because the underlying storage is a byte array, you can see the output 9 bytes, so the length is 9, not the surface of our number 5 (the characters are 5);
4) Chars count:5 indicates that the STR character is 5 long (we need to convert the string (the default is ASCII encoding) to the Rune type for calculation)
5) Chars =[97 98 99 27721 23376]: Indicates that each character of the STR string corresponds to the underlying UTF8 encoding
Second, exercises
Exercise 1 : Write a program to reverse the English string .
Method 1: Complex notation
Package Mainimport ("FMT") func main () {var str string = "ABCDEFG" bytes: = []byte (str)///String characters cannot be modified directly, so we turn this here into a byte array//var I int//i = 0//var i = 0for I: = 0; I < len (bytes)/2; i++ {//To be divided by 2, because one cycle does 2 operations//fmt. Printf ("%c", str[i])//prints out a byte array in a string//by exchanging the first and last switches in reverse order var tmp = Bytes[i] //Assigning the previous assignment to a temporary variable bytes[i] = Bytes[len (bytes)-i-1]//assigns the previous assignment to the latter Bytes[len (bytes)-i-1] = tmp //assigns the latter one to the previous}str = string (bytes)//Converts a byte array to a string fmt. Printf ("Reverse string:%s\n", str)}
Execution Result:
D:\project\src\day1\day-1\base-3\array-2>go Run Main.goreverse STRING:GFEDCBA
Method 2: Advanced notation
Package Mainimport ( "FMT") func main () { var str string = ' abcdefg ' bytes: = []byte (str) for I: = 0; I < ; Len (bytes)/2; i++ { bytes[i], Bytes[len (bytes)-i-1] = Bytes[len (bytes)-i-1], bytes[i] //Two variables take up direct exchange } str = String (bytes)//Converts a byte array to a string fmt. Printf ("Reverse string:%s\n", str)}
Go Language Primer 2--string explanation