The string processing in the go language is not the same as PHP and Java processing, first declaring the string and modifying the string
Copy Code code as follows:
Package Main
Import "FMT"
var name string//declaration of a string
var emptyname string = ""//Declaration of an empty string
Func Main () {
Declare multiple strings and assign values
A, B, V: = "Hello", "word", "Widuu"
Fmt. Println (A, B, v)
Converts the contents of a string by first converting the type A to []byte
c: = []byte (a)
assigning values
C[0] = ' n '
In converting to a string type, we actually find that our A has not changed
But the change of a new string
D: = string (c)
Fmt. Println (d)
String prototype output
M: = ' Hello
Word '
Fmt. Println (M)
}
How to declare an array
Copy Code code as follows:
Package Main
Import "FMT"
var arr [2]int//Declaration of an array
Func Main () {
ARR[0] = 1//array assignment
Fmt. Println (arr)
Arrtest: = [3]int{1, 2, 3}//array another way of declaring
Fmt. Println (Arrtest)
A: = [...] Int{1, 2}//[...] Automatically recognize the length of an array
Fmt. Println (a)
Fmt. Println (Len (a))//output array length
}
Below is the declaration and use of slice in fact, this is a dynamic array
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
D: = []int{1, 2, 3}//Declare a slice this is a dynamic array, no length
Fmt. Println (d)
var q, W []int
Q = d[0:1]//Can be determined to get the length of the top
W = D[1:3]
D = Append (d, 2)//add element to it
Fmt. Println (d)
Fmt. PRINTLN (q, W)
}