Big Integer: reference / value

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?


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.

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

Hi. Try this %p is print value as pointer

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

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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.