Skip to content

Instantly share code, notes, and snippets.

@tio-iis
Created Apr 24, 2022
Embed
What would you like to do?
package main
import "fmt"
func main() {
pointerUser := &User{
name: "tio",
}
fmt.Println(pointerUser.name)
pointerUser.ChangeNameByAtaiReceiver("iis")
fmt.Println(pointerUser.name)
pointerUser.ChangeNameByPointerReceiver("iis")
fmt.Println(pointerUser.name)
}
type User struct {
name string
}
func (u *User) ChangeNameByPointerReceiver(name string) {
//レシーバが参照渡しなので、変更が反映される。
u.name = name
}
func (u User) ChangeNameByAtaiReceiver(name string) {
u.name = name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment