Generally, the values of two pointer types may not be converted to each other if the underlying types of the two pointer types are different. For example, the 4 conversions in the following code are all illegal.
package main
type MyInt int
type IntPtr *int // underlying type is *int
type MyIntPtr *MyInt // underlying type is *MyInt
func main() {
var x IntPtr
var y MyIntPtr
x = IntPtr(y) // error
y = MyIntPtr(x) // error
var _ = (*int)(y) // error
var _ = (*MyInt)(x) // error
}
Although the above 4 conversions may not achieved directly, they may be achieved indirectly. This benefits from the fact that the following conversions are legal.
package main
type MyInt int
func main() {
var x *int
var y *MyInt
x = (*int)(y) // okay
y = (*MyInt)(x) // okay
}
The reason why the above two conversions are legal is values of two unnamed pointers types may be converted to each other's type if the base types of the two types shares the same underlying type. In the above example, the base types of the types of x
and y
are int
and MyInt
, which share the same underlying type int
, so x
and y
may be converted to each other's type.
Benefiting from the just mentioned fact, values of IntPtr
and MyIntPtr
may be also converted to each other's type, though such conversions must be indirectly, as shown in the following code.
package main
type MyInt int
type IntPtr *int
type MyIntPtr *MyInt
func main() {
var x IntPtr
var y MyIntPtr
x = IntPtr((*int)((*MyInt)(y))) // okay
y = MyIntPtr(((*MyInt))((*int)(x))) // okay
var _ = (*int)((*MyInt)(y)) // okay
var _ = (*MyInt)((*int)(x)) // okay
}
The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.
주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.
json.Unmarshal
function accepts case-insensitive object key matches