pointer-new function

new is an in-built function which take type as argument, allocate memory and return a pointer to it

Program:

package main

import (
	"fmt"
)

func modify(p *int) {
	*p = 10
}

func main() {
	p := new(int)
	modify(p)

	// Print value holded by p
	fmt.Println(p)

	// Print value of variable holded by pointer
	fmt.Println(*p)

}

Output:

oxc042004290
10

Leave a Reply

Your email address will not be published. Required fields are marked *