Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a := 5
- // Store the memory address of the variable
- b := &a // 0xc0000140e8
- // Read the variable at a specific memory address
- c := *b // 5
- // Set the variable at a specific memory address
- *b = 10
- fmt.Println(a) // 10
- // Explicit definition
- var d = *int
- d = &a // 0xc0000140e8
- // So what's the point?
- // Unscoped functions can operate directly on a memory address
- // No need for globals
- // Consider...
- func pzero(xPtr *int) {
- *xPtr = 0
- }
- func zero(x int) {
- x = 0
- }
- func main() {
- x := 5
- y := 5
- fmt.Println(x, y) // 5, 5
- pzero(&x)
- zero(y)
- fmt.Println(x, y) // 0, 5
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement