在 Golang 错误中从一个指针到指针类型转换为另一种类型
谁能告诉我为什么这不能编译?
package main
type myint int
func set(a **myint) {
i := myint(5)
*a = &i
}
func main() {
var k *int
set( (**myint)(&k) ) // cannot convert &k (type **int) to type **myint
print( *k )
}
到目前为止我的推理是这样的。 Golang 中的所有类型都是不同的,但只要基础类型相同,它就允许使用类似 C 的强制转换语法从一种类型转换为另一种类型。在我的示例中,将“int”转换为“myint”不是问题。 '*int' 到 '*myint' 也不是。当你有指针指向指针时就会出现问题。我已经被困在这个问题上第二天了。任何帮助表示赞赏。
Can anyone tell my why this wouldn't compile?
package main
type myint int
func set(a **myint) {
i := myint(5)
*a = &i
}
func main() {
var k *int
set( (**myint)(&k) ) // cannot convert &k (type **int) to type **myint
print( *k )
}
My reasoning so far is this. All types in Golang are different, but it allows to convert from one type to another with C-like cast syntax as long as underlying types are identical. In my example, converting 'int' to 'myint' is not a problem. '*int' to '*myint' isn't either. It's when you have pointer to pointer problems arise. I've been stuck on this for the second day now. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的分析。
(**myint)(&k)
-- 无法将&k
(type **int
) 转换为type * *myint
:type **int
和type **myint
是未命名的指针类型及其指针基类型,type *int
和type *myint
没有相同的基础类型。如果 T(
*int
或*myint
)是指针类型文字,则相应的基础类型是 T 本身。(*myint)(k)
-- 可以将k
(type *int
) 转换为type *myint
:type *int
和type *myint
是未命名的指针类型及其指针基类型,type int
和type myint
(type myint int
),具有相同的基础类型。如果 T (
int
) 是预声明类型,则相应的基础类型是 T 本身。如果 T (myint
) 既不是预声明类型,也不是类型文字,则 T 的基础类型是 T 在其类型声明中引用的类型的基础类型 (type myint int
)代码>)。(myint)(*k)
-- 可以将*k
(type int
) 转换为type myint
:< code>type int 和
type myint
具有相同的基础类型。如果 T (
int
) 是预声明类型,则相应的基础类型是 T 本身。如果 T (myint
) 既不是预声明类型,也不是类型文字,则 T 的基础类型是 T 在其类型声明中引用的类型的基础类型 (type myint int
)代码>)。以下是类型部分中的基础类型示例,已修改为使用整数和 int 指针。
int
、T1
和T2
的基础类型是int
。*T1
、T3
和T4
的基础类型是*T1
。参考资料:
Go 编程语言规范
转换
类型
类型和值的属性
类型声明
预声明标识符
指针类型
Here's my analysis.
(**myint)(&k)
-- cannot convert&k
(type **int
) totype **myint
:type **int
andtype **myint
are unnamed pointer types and their pointer base types,type *int
andtype *myint
, don't have identical underlying types.If T (
*int
or*myint
) is a pointer type literal, the corresponding underlying type is T itself.(*myint)(k)
-- can convertk
(type *int
) totype *myint
:type *int
andtype *myint
are unnamed pointer types and their pointer base types,type int
andtype myint
(type myint int
), have identical underlying types.If T (
int
) is a predeclared type, the corresponding underlying type is T itself. If T (myint
) is neither a predeclared type or nor a type literal, T's underlying type is the underlying type of the type to which T refers in its type declaration (type myint int
).(myint)(*k)
-- can convert*k
(type int
) totype myint
:type int
andtype myint
have identical underlying types.If T (
int
) is a predeclared type, the corresponding underlying type is T itself. If T (myint
) is neither a predeclared type or nor a type literal, T's underlying type is the underlying type of the type to which T refers in its type declaration (type myint int
).Here's the underlying type example from the Types section revised to use integers and int pointers.
The underlying type of
int
,T1
, andT2
isint
. The underlying type of*T1
,T3
, andT4
is*T1
.References:
The Go Programming Language Specification
Conversions
Types
Properties of types and values
Type declarations
Predeclared identifiers
Pointer Type
这是您的程序的两个功能等效的工作版本。
http://play.golang.org/p/l_b9LBElie
http://play.golang.org/p/hyaPFUNlp8
Here are two functionally equivalent working versions of your program.
http://play.golang.org/p/l_b9LBElie
http://play.golang.org/p/hyaPFUNlp8