Skip to content

Instantly share code, notes, and snippets.

@yoppi
Created April 4, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoppi/71476033142cddbef989fb621fe5a2ee to your computer and use it in GitHub Desktop.
Save yoppi/71476033142cddbef989fb621fe5a2ee to your computer and use it in GitHub Desktop.
Goのmapは値渡しではない
$ ./map
map[1:hoge]
&map[1:hoge]
map[1:hogehoge]
&map[1:hogehoge]
package main
import "fmt"
type MapT struct {
A map[int]string
B *map[int]string
}
func NewMapT() *MapT {
return &MapT{
A: map[int]string{
1: "hoge",
},
B: &map[int]string{
1: "hoge",
},
}
}
func (m *MapT) GetA() map[int]string {
return m.A
}
func (m *MapT) GetB() *map[int]string {
return m.B
}
func main() {
m := NewMapT()
fmt.Printf("%v\n", m.GetA())
fmt.Printf("%v\n", m.GetB())
a := m.GetA()
b := *m.GetB()
a[1] = "hogehoge"
b[1] = "hogehoge"
fmt.Printf("%v\n", m.GetA())
fmt.Printf("%v\n", m.GetB())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment