Created
October 21, 2022 02:51
-
-
Save saggie/9c6dee14422179f8315888cd35655d3a to your computer and use it in GitHub Desktop.
Go言語の値渡しと参照渡し
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
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