Created
April 4, 2018 06:03
-
-
Save yoppi/71476033142cddbef989fb621fe5a2ee to your computer and use it in GitHub Desktop.
Goのmapは値渡しではない
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./map | |
map[1:hoge] | |
&map[1:hoge] | |
map[1:hogehoge] | |
&map[1:hogehoge] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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