1, the pointer

The pointer thing, which we have in C, is the location in memory where a variable is stored.

In GO, Pointers cannot be operated on. (PS: I think this is very good, I sometimes avoid pointer operation when WRITING C, otherwise it is easy to make mistakes, although pointer operation is also very convenient, but I do not use it.)

Package main import "FMT" func main() {a:=100 ftt.printf ("%x\n",&a) var b *int=&a ftt.println (b) var b *int=&a ftt.println (b) Println(*b) // Return the address of a 0xC00000A0a8 fmt.println (&b) // Return the address of b 0xC000006030 fmt.println (*b)Copy the code

In GO, when we declare a pointer p to a NULL address, we call that pointer NULL,nil

Sample1 swaps variables with Pointers

package main import "fmt" func swap(a,b *int) { tmp:=*a *a=*b *b=tmp } func main() { a:=100 b:=200 c :=&a d:=&b FMT. Println (* c * d) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") swap (c, d) FMT. Println (* c * d)} / * return 100 200 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 200 100 * /Copy the code

1.1 Pointer Array

An array of Pointers is just an array of Pointers. This thing is especially useful in C sometimes, such as temporarily creating an array of Pointers in stack memory to act as a queue or stack.

Sample iterates through elements using an array of Pointers

Package main import "FMT" const count int =4 func main() {a:=[count] string {" ABC ","1","1.2"," 567 "} I :=0 // define pointer array var PTR [count]*string // Assign each element address to the array of Pointers for I =0; i<count; I ++{PTR [I]=&a[I]} for I =0; i<count; i++{ fmt.Println(*ptr[i]) } }Copy the code

1.2 Pointers of Pointers

Package main import "FMT" func main() {a:=100 // create an int a=100 p:=&a // pointer p store a address pp:=&p // pointer pointer store P address FMT. Printf (" % d, % d \ n ", & a, a) / / print the address of a, a, the value of the FMT) Printf (" % d, % d \ n ", p, * p) / / print p deposit address, Printf("%d,%d,%d\n",pp,*pp,**pp) //pp is the address of pp,*pp is the address of pp pointer p, * * pp is the value of the pp - > p - > a} / / a output the result of the value of / * 824633761960100, 824633761960100, 824633745448824337196, 0100 * /Copy the code

Sample defines Pointers and Pointers to Pointers

Println(PPTR, PPTR, PPTR, PPTR, PPTR, PPTR, PPTR, PPTR, PPTR); Println(PTR) // Prints the address of aCopy the code

1.3 Parameter Transfer

2, arrays,

2.1 Definition of arrays

Var a = [length] type {, element1 element2} var a = [2] int {1, 2}Copy the code

The array of GO is different from the array of C is that the array of C is Pointers, which is the address of the array of C is the address of the first element of the array. In GO you take the address of the array and you get the entire array, so you can’t go through the array with a pointer

2.2 Array length

Get the length of the array in go by len().

Var a = [2] int {2, 4}; FMT.Println(len(a)) // here output 2Copy the code

2.3 Multidimensional Array

/ / create a two dimensional array of three lines of four columns var a = [3] [4] int {{1, 2, 3, 4}, {2, 5-tetrafluorobenzoic}, {3,4,5,6}}Copy the code

Arrays are value types in go. That is, the array allocation is stored on the stack

3, slicingFocus on

A slice is a mutable array, kind of like a vector in C++, right? The slice structure can be understood as three parts: 1. Pointer PTR, pointing to the specified position of the slice. 2. Length Len, length of slice. 3. Capacity Cap, that is, the length of the slice from the beginning to the end. Slice’s syntax is similar to that of an array, except that there is no fixed length.

Sliced data structures

The slice PTR points to the first element

Typedef struct d{int * PTR; // A pointer to a number in the array int len; // Here is the length, the length of the slice int cap; // Here is the size of the section}Copy the code

The difference between length and capacity is, length is what you put in the elements, capacity is how much you can put in?

The relationship between slices and arrays is even defined the same way.

package main

import "fmt"

func main() {
    months := []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    fmt.Println(months)
    fmt.Println("Length:", len(months))
    fmt.Println("Capacity:", cap(months))
}
Copy the code

Slice item (key point)

package main import "fmt" func main() { months := []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} quarter1 := months[0:3] // Here is section 1 quarter2 := months[3:6] // here is section 2 quarter3 := months[6:9] // here is section 3 quarter4 Println(quarter1, len(quarter1)) Println(quarter2, len(quarter2)) cap(quarter2)) fmt.Println(quarter3, len(quarter3), cap(quarter3)) fmt.Println(quarter4, len(quarter4), cap(quarter4)) }Copy the code

3.1 Section Expansion (*)

Slice expansion can be said to be a knowledge point worth paying attention to.

Package main import "FMT" func main() {a:=[]int{1,3,5,7} fmt.Printf(" Lena :%d\tcapa:%d\n",len(a),cap(a)) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- ") : b = append (a, 1, 2) FMT. Printf (" \ tcapb lenb: % d: % d \ n ", len (b), cap (b)) / / b here expansion for two times C: = append (a, 2,4,5,6,7) FMT. Printf (" \ tcapc lenc: % d: % d \ n ", len (c), cap (c)) / / c here due to the expansion cap < = = 8 len = = 9, so cap = = 10 D: append = (a, 1,2,3,4,5,6,7) FMT. Printf (" \ tcapd lend: % d: % d \ n ", len (d), cap (d)) / / same as above here} / * output Lena: 4 capa: 4 ------------- lenb:6 capb:8 lenc:9 capc:10 lend:11 capd:12 */Copy the code

Sample inverts the array

Package main import "FMT" func main() {a:=[]int{1,3,5,7,9} for I,j:=0,len(a)-1; i<j; I, j = I + 1, j - 1 {a [I], a [j] = [j], a [I]} FMT. Println} / * (a) returns the [9, 7 5 3 1] * /Copy the code

3.2 equal

Use equal to compare two slices

func equal(x, y []string) bool { if len(x) ! = len(y) { return false } for i := range x { if x[i] ! = y[i] { return false } } return true }Copy the code

Slicing does not have any data of its own. It’s just a reference to the underlying array. Any changes made to the slice will be reflected in the underlying array. Arrays are value types and slices are reference types

3.3 copy and append

Copy copy

Package the main import (FMT) func main () {a: = [] int,3,5,7,9 {1} / / b: = [] int,3,5,7,8 {1} : b = make (int [], len (a), cap (a) * 2) ret:=copy(b,a) fmt.Printf("%x,%x\n",&a,&b) fmt.Println(ret) fmt.Println(b) }Copy the code