You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
389 B
31 lines
389 B
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
m := MyStruct{
|
|
x: "struct",
|
|
y: 42,
|
|
}
|
|
|
|
fmt.Println(m) // {struct 42}
|
|
mutateNoPointer(m)
|
|
fmt.Println(m) // {struct 42}
|
|
mutatePointer(&m)
|
|
fmt.Println(m) // {changed 0}
|
|
}
|
|
|
|
type MyStruct struct {
|
|
x string
|
|
y int
|
|
}
|
|
|
|
func mutateNoPointer(m MyStruct) {
|
|
m.x = "changed"
|
|
m.y = 0
|
|
}
|
|
func mutatePointer(m *MyStruct) {
|
|
m.x = "changed"
|
|
m.y = 0
|
|
}
|