Created
June 29, 2018 09:42
-
-
Save noel9999/f1c04d4a36483bba0c12d88bea791fc6 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
package main | |
import("fmt") | |
type Student struct { | |
id int64 | |
name string | |
} | |
func main() { | |
s := Student { 123, "Yoda" } | |
fmt.Printf("s value is %s, address is %p, and type is %T\n", s, &s, s) | |
updateStudent(&s) | |
fmt.Printf("s value is %s, address is %p, and type is %T\n", s, &s, s) | |
} | |
/* 輸出 | |
s value is {%!s(int64=123) Yoda}, address is 0xc420096040, and type is main.Student | |
s value is 0xc420096040, address is 0xc42009a020, object is &{456 Vader}, and type is *main.Student | |
s value is {%!s(int64=456) Vader}, address is 0xc420096040, and type is main.Student | |
*/ | |
func updateStudent(s *Student) { | |
// 給他用正宗的指標取值方式 | |
(*s).name = "Vader" | |
// OverHere!!! | |
// 但這麼做實際上也可以直接操作地址參考的內容,而且大多的 go 範例都這樣寫,不明絕厲 | |
s.id = 456 | |
fmt.Printf("s value is %p, address is %p, object is %v, and type is %T\n", s, &s, s, s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment