Last active
June 24, 2020 22:57
-
-
Save sprytnyk/5ab0a65bfdc045f3fea502d9fbc36117 to your computer and use it in GitHub Desktop.
How pointers works in Golang.
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 main() { | |
/* | |
i initial value is 0 | |
j initial value is 0 | |
i initial address is 0xc0000b4010 | |
j initial address is 0xc0000b4018 | |
i address is 0xc0000b4010 | |
i value is 0 | |
p value is 0xc0000b4010 | |
p address is 0xc0000b6020 | |
j address is 0xc0000b4010 | |
j value is 0 | |
p value is 0xc0000b4018 | |
p address is 0xc0000b6020 | |
*/ | |
i, j := 0, 0 | |
fmt.Printf("i initial value is %d\n", i) | |
fmt.Printf("j initial value is %d\n", j) | |
fmt.Printf("i initial address is %p\n", &i) | |
fmt.Printf("j initial address is %v\n", &j) | |
fmt.Println() | |
p := &i | |
fmt.Printf("i address is %p\n", &i) | |
fmt.Printf("i value is %d\n", i) | |
fmt.Printf("p value is %v\n", p) | |
fmt.Printf("p address is %p\n", &p) | |
fmt.Println() | |
p = &j | |
fmt.Printf("j address is %p\n", &i) | |
fmt.Printf("j value is %d\n", j) | |
fmt.Printf("p value is %v\n", p) | |
fmt.Printf("p address is %p\n", &p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment