Skip to content

Instantly share code, notes, and snippets.

@yishingene
Last active July 28, 2022 02:15
Show Gist options
  • Select an option

  • Save yishingene/5137b7df3eee9e1f3f192456a030663e to your computer and use it in GitHub Desktop.

Select an option

Save yishingene/5137b7df3eee9e1f3f192456a030663e to your computer and use it in GitHub Desktop.
Golang pointer
package main
import "fmt"
type person struct {
name string
}
func main() {
p := person{"Richard"}
fmt.Printf("Before rename,address: %p\n", &p)
fmt.Printf("Before renam, p value: %s\n", p)
rename(&p)
fmt.Printf("After rename, address of p: %p\n", &p)
fmt.Printf("After rename, address of p: %s\n", p)
}
func rename(p *person) { // 使用 pointer, 可以想成綁住 "指定位置" 的person variable
p.name = "test"
fmt.Printf("Within rename, p value, %s\n", p)
fmt.Printf("Within rename, address of p, %p\n", p) // 不要使用 &p , 因為此時 p 已經是 pointer , 直接印出地址, 使用 &p ,會變成 pointer of pointer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment