Created
February 10, 2022 01:19
-
-
Save kougazhang/cf295ab93256b536dd9f96b9ea9bca67 to your computer and use it in GitHub Desktop.
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
type A struct { | |
B int | |
} | |
func TestParserResult_Accept(t *testing.T) { | |
a := &A{ | |
B: 1, | |
} | |
fmt.Printf("bf: %p\n", a) | |
b(a) | |
fmt.Println(a) | |
c(&a) | |
fmt.Println(a) | |
} | |
func b(x *A) { | |
fmt.Printf("fn: %p\n", x) | |
// 修改失败 | |
x = nil | |
} | |
// 二级指针可以修改传入的指针类型的参数 | |
func c(x **A) { | |
fmt.Printf("xn: %p\n", *x) | |
// 修改参数 | |
*x = nil | |
} | |
// 指针作为函数参数会拷贝一份指针地址,所以在 `b` 中修改 `x = nil` 操作是指针副本,所以修改无效。 | |
// 在 `c` 中修改 `*x=nil` 操作,修改的是指向 `*A` 的指针 a,`a` 的值未发生拷贝,所以可以修改。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment