Pointer
This chapter focuses on the application of string, number, array, slice, map, channel, structure and pointer assignment and function parameter transfer.
Character string
The string itself is also the structure of StringHeader, which contains the Data pointer and string length, as follows
type StringHeader struct { Data uintptr Len int }
Data points to memory addresses that are not changeable, string assignments and parameters just copy the values of Data and Len in StringHeader
package main import "fmt" func main() { str := "Hello World!" var data string data = str // Copy Data and Len in str fmt.Println(data) data = "Hello Go!" // Modify the value of data to generate a new StringHeader structure in memory and assign it to data fmt.Println(data) // str memory unchanged fmt.Println(str) } //Hello World! //Hello Go! //Hello World!
When a variable is declared as a string pointer, the object must be the memory address of the string when the variable is assigned, and the memory address is only copied when the function is passed parameters.
package main import "fmt" func main() { str := "Hello World!" var ptr *string // The error method str is a string type, not a pointer type //ptr = str // Get the address of str correctly and assign it to ptr ptr = &str fmt.Println(ptr) fmt.Println(*ptr) } //0xc0000421c0 //Hello World!
Figures
Numbers do not find the corresponding structure. Number type assignment, pointer, function transfer are consistent with the string and can not be modified (modification is equivalent to reassignment).
package main import "fmt" func main() { number := 10 var ptr *int // The error method str is a string type, not a pointer type //ptr = str // Get the address of str correctly and assign it to ptr ptr = &number fmt.Println(ptr) fmt.Println(*ptr) } //0xc00000a0b8 //10
Array
Array assignment and function parameter transfer are all memory copies, data will be copied, the new array modification does not affect the assigned array.
package main import "fmt" func main() { list1 := [4] int{1,2,3,4} list2 := list1 list2[0] =100 fmt.Println(list1) fmt.Println(list2) } //[1 2 3 4] //[100 2 3 4]
Array pointer, needn't add * when the pointer is modified; when modified, the original array is changed
package main import "fmt" func main() { list := [4]int{1,2,3,4} // Specify the size of the array when declaring var ptr *[4]int ptr = &list // Erroneous assignment //*ptr[0] =100 // Correct way ptr[0] = 100 fmt.Println(list) fmt.Println(*ptr) } //[100 2 3 4] //[100 2 3 4]
Section
Slice Header is as follows
type SliceHeader struct { // When pointing to the array memory address assignment, the array address is copied. Data uintptr // length Len int // Application space Cap int }
When assigning, copy ing, and function parameters, only the variables in the structure are copied. details Go Language [Data Structure] Slice
package main import "fmt" func main() { slice1 := []int{1,2,3,4} slice2 := slice1 slice2[0] =100 fmt.Println(slice1) fmt.Println(slice2) } //[100 2 3 4] //[100 2 3 4]
Slice pointer, the following way can not be modified
package main import "fmt" func main() { slice1 := []int{1,2,3,4} var ptr *[]int ptr = &slice1 // Neither of the following can be modified ptr[0] =100 *ptr[0] =100 fmt.Println(slice1) fmt.Println(*ptr) } //[1 2 3 4] //[1 2 3 4]
channel
Channels are assigned to the same memory address
package main import "fmt" func main() { chan1 := make(chan string,1) chan2 := chan1 chan2 <- "hello" data := <-chan1 fmt.Println(data) // Point to the same address fmt.Println(chan1) fmt.Println(chan2) } //hello //0xc000088000 //0xc000088000
Meanwhile, channels also support pointers
package main import "fmt" func main() { chan1 := make(chan string,1) chan2 := &chan1 *chan2 <- "hello" data := <-chan1 fmt.Println(data) fmt.Println(chan1) fmt.Println(chan2) } //hello //0xc000038060 //0xc000006028
Structure
So the key point is, what is the difference between the structure assignment and the pointer? Let's first look at three ways to generate the theatre structure. The second way is the same as the third way.
package main import "fmt" type Phone struct { color string name string } func main() { // The first generation method generates structured objects phone1 :=Phone{"Red","Iphone"} fmt.Println(phone1.color) fmt.Println(phone1.name) fmt.Println(phone1) // The second generation method generates structure pointer phone2 :=&Phone{"Red","Iphone"} fmt.Println(phone2.color) fmt.Println(phone2.name) fmt.Println(phone2) // The third generation method generates structure pointer phone3 := new(Phone) phone3.color = "Red" phone3.name = "Iphone" fmt.Println(phone3.color) fmt.Println(phone3.name) fmt.Println(phone3) } //Red //Iphone //{Red Iphone} //Red //Iphone //&{Red Iphone} //Red //Iphone //&{Red Iphone}
The structure assignment is equivalent to copying the variables in the structure, and the function transfer takes part in the assignment as well.
package main import "fmt" type Phone struct { color string name string } func main() { // assignment phone1 :=Phone{"Red","Iphone"} phone2 := phone1 phone2.color = "Green" fmt.Println(phone1.color) fmt.Println(phone2.color) } //Red //Green
The pointer only copies the memory address of the structure, and the modification will affect the original value.
package main import "fmt" type Phone struct { color string name string } func main() { // assignment phone1 :=&Phone{"Red","Iphone"} phone2 := phone1 phone2.color = "Green" fmt.Println(phone1.color) fmt.Println(phone2.color) } //Green //Green