Skip to content

Instantly share code, notes, and snippets.

@rsj217
Created July 22, 2017 09:53
Show Gist options
  • Save rsj217/5bc1bbb1770dafaf02f10db70e9202d6 to your computer and use it in GitHub Desktop.
Save rsj217/5bc1bbb1770dafaf02f10db70e9202d6 to your computer and use it in GitHub Desktop.
go-poiter-new-make-nil
package main
import "fmt"
func main() {
// 声明一个变量 aVar 类型为 string
var aVar string
fmt.Printf("aVar: %p %#v\n", &aVar, aVar) // 输出 aVar: 0xc42000e240 ""
aVar = "This is a aVar"
fmt.Printf("aVar: %p %#v\n", &aVar, aVar) // 输出 aVar: 0xc42000e240 ""
// 声明一个指针变量 aPot 其类型也是 string
var aPot *string
fmt.Printf("aPot: %p %#v\n", &aPot, aPot) // 输出 aPot: 0xc42000c030 (*string)(nil)
aPot = &aVar
*aPot = "This is a Pointer"
fmt.Printf("aVar: %p %#v \n", &aVar, aVar) // 输出 aVar: 0xc42000e240 "This is a Pointer"
fmt.Printf("aPot: %p %#v %#v \n", &aPot, aPot, *aPot) // 输出 aPot: 0xc42000c030 (*string)(0xc42000e240) "This is a Pointer"
// 声明另外一个指针变量
var aNewPot *int
// 使用new开辟一块内存
aNewPot = new(int)
*aNewPot = 217
fmt.Printf("aNewPot: %p %#v %#v \n", &aNewPot, aNewPot, *aNewPot) // 输出 aNewPot: 0xc42007a028 (*int)(0xc42006e1f0) 217
// 声明一个数组
var arr [5]int
fmt.Printf("arr: %p %#v \n", &arr, arr) // arr: 0xc420014180 [5]int{0, 0, 0, 0, 0}
arr[0], arr[1] = 1, 2
fmt.Printf("arr: %p %#v \n", &arr, arr) // arr: 0xc420014180 [5]int{1, 2, 0, 0, 0}
// 声明一个指针数组
var arrPot *[5]int
fmt.Printf("arrPot: %p %#v \n", &arrPot, arrPot) // arrPot: 0xc42000c040 (*[5]int)(nil)
// 给指针数组初始化其值,即开辟一个内存并返回地址
arrPot = new([5]int)
fmt.Printf("arrPot: %p %#v \n", &arrPot, arrPot) // arrPot: 0xc42000c040 &[5]int{0, 0, 0, 0, 0}
(*arrPot)[0] = 11
fmt.Printf("arrPot: %p %#v \n", &arrPot, arrPot) // arrPot: 0xc42000c040 &[5]int{11, 0, 0, 0, 0}
// 声明一个map
var aMap map[string]string
fmt.Printf("aMap: %p %#v \n", &aMap, aMap) // aMap: 0xc42000c048 map[string]string(nil)
//&aMap = new(map[string]string) // 报错: cannot assign to &aMap
// 使用make初始化一个map内存,并返回该map
aMap = make(map[string]string)
aMap["name"] = "Golang"
fmt.Printf("aMap: %p %#v \n", &aMap, aMap) // aMap: 0xc420078038 map[string]string{"name":"Golang"}
// 声明一个map类型的指针
var mapPot *map[string]int
fmt.Printf("mapPot: %p %#v \n", &mapPot, mapPot) // mapPot: 0xc42000c050 (*map[string]int)(nil)
// 初始化map指针的地址
mapPot = new(map[string]int)
fmt.Printf("mapPot: %p %#v \n", &mapPot, mapPot) // mapPot: 0xc42000c050 &map[string]int(nil)
//(*mapPot)["age"] = 21 // 报错
// 初始化map指针指向的map
(*mapPot) = make(map[string]int)
(*mapPot)["age"] = 21
fmt.Printf("mapPot: %p %#v \n", &mapPot, mapPot) // mapPot: 0xc42000c050 &map[string]int{"age":21}
// 定义一个基于map的自定义类型
type User map[string]string
// 初始化,其零值为nil
var user User
fmt.Printf("user: %p %#v \n", &user, user) // user: 0xc42000c060 main.User(nil)
user = make(User)
user["name"] = "Golang"
fmt.Printf("user: %p %#v \n", &user, user) // user: 0xc42007a050 main.User{"name":"Golang"}
var userPot *User
fmt.Printf("userPot: %p %#v \n", &userPot, userPot) // userPot: 0xc42000c068 (*main.User)(nil)
userPot = new(User)
fmt.Printf("userPot: %p %#v \n", &userPot, userPot) // userPot: 0xc42000c068 &main.User(nil)
(*userPot) = make(User)
fmt.Printf("userPot: %p %#v \n", &userPot, userPot) // userPot: 0xc42000c068 &main.User{}
(*userPot)["name"] = "Golang"
fmt.Printf("userPot: %p %#v \n", &userPot, userPot) // userPot: 0xc42000c068 &main.User{"name":"Golang"}
person := Person{"vanyar", 21}
fmt.Printf("person<%s:%d>\n", person.name, person.age)
person.sayHi()
// 值接受者
person.ModifyAge(210)
person.sayHi()
// 指针接受者
person.ChangeAge(210)
person.sayHi()
personPot := &Person{"noldor", 27}
fmt.Printf("personPot<%s:%d>\n", personPot.name, personPot.age)
personPot.sayHi()
personPot.ModifyAge(270)
personPot.sayHi()
personPot.ChangeAge(270)
personPot.sayHi()
}
type Person struct {
name string
age int
}
func (p Person) sayHi() {
fmt.Printf("SayHi -- This is %s, my age is %d\n",p.name, p.age)
}
func (p Person) ModifyAge(age int) {
fmt.Printf("ModifyAge")
p.age = age
}
func (p *Person) ChangeAge(age int) {
fmt.Printf("ModifyAge")
p.age = age
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment