Big Integer: reference / value

(Shawn) #1

Hi there,

I have a question regarding the use of big int. The following is the program and the output:

func main() {
	a := big.NewInt(1000)

// {false [1000]}
// 1000

I am wondering why the reference of the big int is the value I passed in?


(Lutz Horn) #2

big.NewInt returns an *Int. The source code of this struct type shows us this:

type Int struct {
	neg bool // sign
	abs nat  // absolute value of the integer

So printing *a outputs neg and abs.

(Shawn) #3

Thank you for your help. I understand that printing *a returns both neg and abs. However, for printing a, I thought it suppose to print out its address instead of the value 1000 because big.NewInt creates a pointer to the structure Int

(Johan Dahl) #4

Hi. Try this %p is print value as pointer

fmt.Printf("%p\n", a)

(Sean Killian) #5

Hi, Shawn, you’re seeing the big integer’s value instead of the pointer because *big.Int has a String method on it and according to the default formatting rules in the Printing section of the fmt package’s Overview, having that member function causes the default formatting to change to call that function.