Skip to content

Instantly share code, notes, and snippets.

@ProProgrammer
Last active September 29, 2018 14:15
Show Gist options
  • Save ProProgrammer/c292852332249b1aa931b4956273aa99 to your computer and use it in GitHub Desktop.
Save ProProgrammer/c292852332249b1aa931b4956273aa99 to your computer and use it in GitHub Desktop.
Practicing pointers in Go based on explanation by Shawn here: https://www.youtube.com/watch?v=hMSYabOnA3M
package main
import "fmt"
func main() {
x := 42
fmt.Println("Address of x inside main:", &x)
fmt.Println("Initial value of x inside main before any external functions are called:", x)
fmt.Println("---------------")
print(x)
fmt.Println("After print(x), value of x inside main():", x)
fmt.Println("---------------")
printwithpointer(&x)
fmt.Println("After printwithpointer(x), value of x inside main():", x)
fmt.Println("---------------")
fmt.Println("Final value of x inside main AFTER print and printwithpointer (which modified the value of x inside main()) functions are called:", x)
}
func print(x int) {
x += 10
fmt.Println("Address of x inside print:", &x)
fmt.Println("value of x inside print:", x)
}
func printwithpointer(x *int) {
*x += 10
fmt.Println("Address of x inside printwithpointer:", &x)
fmt.Println("value of *x inside printwithpointer:", *x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment