在()中包装类型有什么作用,什么时候应该使用它? Golang [重复]

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.