What exactly is this "cast" doing in Golang?

999 views Asked by At

Can someone point me in the right direction of this Go's syntax :

(*int)(nil)

If I have a a value of a given type and I want to convert it to, lets say, float64 I can do this :

var num int = 65
fnum := float64(num)

If I have an interface and I want to "cast it" to some type I can do this :

func main() {
    concretevalue := dosomething("hello!")
    fmt.Printf("%T : %v", concretevalue, concretevalue)
}

func dosomething( v interface{} ) string {
    return v.(string)
}

Where does the (*int)(nil) fit? How can I get information about this specific syntax?

1

There are 1 answers

0
Burak Serdar On BEST ANSWER

It is a type-conversion, same as float64(num), however, because the converted type is a pointer, you need extra parentheses, because otherwise *int(nil) would mean converting nil to int, and then dereferencing it.