Go语言指针

2018-09-27  本文已影响0人  AuglyXu

只讲述和C语言地址的不同

指向数组的指针

var arr [3]int = [3]int{1,3,5}
var p *[3]int = &arr
//用地址操作数组
//arr[0] = 666
(*p)[0] = 666

指向切片的指针

var sce []int = []int{1, 3, 5}
var p *[]int = &sce
//操作切片的数据
(*p)[0] = 666

指向字典的指针

var dict map[string]string = map[string]string{"name":"lnj","age":"18"}
var p *map[string]string = &dict
(*p)["name"] = "zs"
fmt.Println(dict)//{"name":"zs","age":"18"}

指向结构体的指针

    type Person struct {
        name string
        age int
    }
    var per Person = Person{"lnj", 18}
    var p *Person
    p = &per
    //(*p).name = "zs"
    p.name = "zs"
    fmt.Println(per)

指针作为返回值

上一篇下一篇

猜你喜欢

热点阅读