Skip to content

Instantly share code, notes, and snippets.

@saggie
Created October 21, 2022 02:51
Show Gist options
  • Save saggie/9c6dee14422179f8315888cd35655d3a to your computer and use it in GitHub Desktop.
Save saggie/9c6dee14422179f8315888cd35655d3a to your computer and use it in GitHub Desktop.
Go言語の値渡しと参照渡し
func main() {
someValue := 1111
// 値渡し
passAsValue(someValue)
fmt.Println(someValue) // 1111 (変わらない)
// 参照渡し
passAsReference(&someValue)
fmt.Println(someValue) // 2222 (変わった!)
}
func passAsValue(someValue int) {
someValue += 1111
}
func passAsReference(someValue *int) {
*someValue += 1111
// ここで someValue += 1111 とするとエラー
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment