Last active
June 12, 2019 17:07
-
-
Save ProProgrammer/b41501a5c6c1a8db621f22df2a54c1e7 to your computer and use it in GitHub Desktop.
Trying to understand pointers in Golang while going through Golang Tour
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
package main | |
import "fmt" | |
func main() { | |
a := 200 | |
b := &a // Point to memory address of variable a | |
fmt.Println("Initial value of variable a:", a) | |
fmt.Println("b := &a, hence value of b:", b) | |
fmt.Println("Value of *b:", *b) // You can access the value in memory location to whicht he value points using asterisk before the variable name | |
// Let us increment the value in the memory to which variable b points. | |
*b++ // this is called deferencing b (i.e. follow the pointer from b to a) | |
fmt.Println("Value of *b after running *b++:", *b) | |
// Since *b++ incremented the value in the memory location to which variable b points, any other variable that points to same memory location should also now have the new value same as that of "*b" | |
fmt.Println("Value of variable a:", a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment