-1

I was playing around with various packages and data types and I came across this function(big.NewInt()) from "math/big" package. so this function returns a pointer to the type bigInt but when i print it out i get the actual value not the address where it is stored can anyone help me understand this better?

package main

import "fmt"
import "math/big"
import "reflect"

func main(){
    target := big.NewInt(1)
    a := 5
    s := &a
    fmt.Println(reflect.TypeOf(target))
    fmt.Println(reflect.TypeOf(s))
    fmt.Println(s)
    fmt.Println(target)
}
JimB
  • 104,193
  • 13
  • 262
  • 255
Chandru
  • 467
  • 1
  • 8
  • 17
  • 4
    This is a feature of fmt. Which invokes the appropriate String function and has nothing to do withe pointers or big.Int. – Volker Jun 04 '20 at 12:36
  • 2
    https://pkg.go.dev/fmt/?tab=doc#Stringer – JimB Jun 04 '20 at 12:38
  • 1
    but y does it automatically print the address in case of a integer pointer and not in case of a big int pointer – Chandru Jun 04 '20 at 12:40
  • 6
    @Chandru: because an `*int` does not have a `String()` method to make it a `fmt.Stringer`. – JimB Jun 04 '20 at 12:48

1 Answers1

1
fmt.Printf("%p\n", target)      // 0xc00000c080
aMike
  • 852
  • 5
  • 14
  • which value does the target hold?"0xc000000c080" or 1? – Chandru Jun 04 '20 at 12:41
  • It printed '1' before it the address. And this address was "around" the first address that it printed. My test was just a copy and paste of your example with that one line added. – aMike Jun 04 '20 at 13:26