Created
May 14, 2017 02:30
-
-
Save ifukazoo/52b4c256f9c6aa4c08846ad133bf8a29 to your computer and use it in GitHub Desktop.
goのslice,mapを引数にする場合の挙動
This file contains hidden or 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" | |
func f(m map[string]int) map[string]int { | |
// 呼び出し元も変更 | |
m["a"]++ | |
m["b"]++ | |
return m | |
} | |
func g(m []string) []string { | |
// 呼び出し元は変更されない | |
m = append(m, "a") | |
m = append(m, "b") | |
return m | |
} | |
func h(m []string) []string { | |
// 呼び出し元も変更 | |
m[0] = "a" | |
m[1] = "b" | |
return m | |
} | |
func main() { | |
// m := make(map[string]int) | |
// f(m) | |
// m := []string{} | |
// g(m) | |
m := make([]string, 2) | |
h(m) | |
fmt.Printf("%v\n", m) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment