This question already has an answer here:
- Is this casting in golang? 1 answer
What exactly does wrapping a type in () do exactly and when should I use it? E.g. blah.(*int)
</div>
This question already has an answer here:
What exactly does wrapping a type in () do exactly and when should I use it? E.g. blah.(*int)
</div>
It's a type assertion. Type assertions are used to extract the value in an interface type as some other type.
The expression blah.(*int)
asserts that the type of the value in blah
is *int
. If the assertion holds, then the value of the expression is the value stored in blah
as a *int
. If the assertion does not hold, then the expression panics.
A special form of a type assertion can be used in an assignment to test the assertion:
ip, ok := blah.(*int)
If the value in blah is of *int
, then the value is stored in ip
and ok
is set to true. Otherwise, ip
is set to the zero value and ok
is false.