Declare Pointer in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import "fmt"

func main() {
	Demo1()
	Demo2()
	Demo3()
}

func Demo1() {
	var a int = 5
	var p *int = &a
	fmt.Println("Value of a variable: ", a)
	fmt.Println("Address of a variable: ", &a)
	fmt.Println("Address of p pointer: ", p)
	fmt.Println("Value of p pointer: ", *p)

	*p = 6
	fmt.Println("Value of a variable: ", a)
	fmt.Println("Address of a variable: ", &a)
	fmt.Println("Address of p pointer: ", p)
	fmt.Println("Value of p pointer: ", *p)
}

func Demo2() {
	var a int = 5
	p := &a
	fmt.Println("Value of a variable: ", a)
	fmt.Println("Address of a variable: ", &a)
	fmt.Println("Address of p pointer: ", p)
	fmt.Println("Value of p pointer: ", *p)

	*p = 6
	fmt.Println("Value of a variable: ", a)
	fmt.Println("Address of a variable: ", &a)
	fmt.Println("Address of p pointer: ", p)
	fmt.Println("Value of p pointer: ", *p)
}

func Demo3() {
	var a int = 5
	var b int = 10
	p, q := &a, &b

	fmt.Println("Value of a variable: ", a)
	fmt.Println("Address of a variable: ", &a)
	fmt.Println("Value of b variable: ", b)
	fmt.Println("Address of b variable: ", &b)

	fmt.Println("Address of p pointer: ", p)
	fmt.Println("Value of p pointer: ", *p)
	fmt.Println("Address of q pointer: ", q)
	fmt.Println("Value of q pointer: ", *q)
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Value of a variable: 5
Address of a variable: 0xc000052058
Address of p pointer: 0xc000052058
Value of p pointer: 5

Value of a variable: 6
Address of a variable: 0xc000052058
Address of p pointer: 0xc000052058
Value of p pointer: 6

Value of a variable: 5
Address of a variable: 0xc0000520a0
Address of p pointer: 0xc0000520a0
Value of p pointer: 5

Value of a variable:  6
Address of a variable: 0xc0000520a0
Address of p pointer: 0xc0000520a0
Value of p pointer: 6

Value of a variable:  5
Address of a variable:  0xc000052058
Value of b variable:  10
Address of b variable:  0xc000052070
Address of p pointer:  0xc000052058
Value of p pointer:  5
Address of q pointer:  0xc000052070
Value of q pointer:  10