1 func pointer_test () {2 //null pointer, nil output3 varP *int4Fmt. Printf ("P:%v\n", p)5 //point to local variable, the value of the variable is initially 06 varIint7p = &I8Fmt. Printf ("P:%v,%v\n", p, *p)9 //modifying variable values with pointersTen*p =8 OneFmt. Printf ("P:%v,%v\n", p, *p) A //initialization and output of arrays -M: = [3]int{3,4,5} -Fmt. Printf ("m:%v--%v,%v,%v\n", M, m[0], m[1], m[2]) the //initialization and output of pointer arrays - //J, K, L: = 3, 4, 5 - //x: = [3]*int{&j, &k, &l} -x: = [3]*int{&m[0], &m[1], &m[2]} +Fmt. Printf ("x:%v,%v,%v\n", x[0], x[1], x[2]) -Fmt. Printf ("*x:%v,%v,%v\n", *x[0], *x[1], *x[2]) + A varn [3]*int atn =x -Fmt. Printf ("n:%v,%v,%v\n", n[0], n[1], n[2]) - //pointer to an array, which is the use of a level two pointer -Y: = []*[3]*int{&x} -Fmt. Printf ("y:%v,%v\n", Y, y[0]) -Fmt. Printf ("*y[0]:%v\n", *y[0]) inFmt. Printf ("*y[][]:%v,%v,%v\n", *y[0][0], *y[0][1], *y[0][2]) - /* to *x v + [3]x] [v0][v1][v2] - []x--[V0] ... [VI] the [3]*x] [P0][P1][P2] * | | | $ J k LPanax Notoginseng []*[3]*x--[N0] ... [NI] - | ... | the [P0]->j + [P1]->k A [P2]->l the */ + } - $Type Studentstruct { $Namestring -Idint -ScoreUINT the } - Wuyi func memery_test () { the //the data assigned by new is in the form of pointers . -P: =New(Student) WuP.name =" China" -P.id =63333 AboutP.score = About $Fmt. PRINTLN (*p) - //var defines a variable as a numeric form - varSt Student -St.name ="Chinese" ASt.id =666333 +St.score = - the FMT. Println (ST) - //Make allocates space for slice, map, and channel, and returns not a pointer $ varPTR *[]student theFmt. Println (PTR)//ptr = = Nil thePTR =New([]student)//point to an empty slice the FMT. Println (PTR) the*ptr = Make ([]student,3, -) - FMT. Println (PTR) inStu: = []student{{" China",3333, the}, {"Chinese",4444, the}, {"chince",5555, the}} the FMT. Println (Stu) the}
Golang learning pointers, memory allocations