2 回答
TA贡献1841条经验 获得超3个赞
你只想要rune(i). 铸造是通过type(x).
类型断言是不同的。当您需要从不太具体的类型(如interface{})转换为更具体的类型时,您可以使用类型断言。此外,在编译时检查强制转换,其中类型断言发生在运行时。
以下是您如何使用类型断言:
var (
x interface{}
y int
z string
)
x = 3
// x is now essentially boxed. Its type is interface{}, but it contains an int.
// This is somewhat analogous to the Object type in other languages
// (though not exactly).
y = x.(int) // succeeds
z = x.(string) // compiles, but fails at runtime
TA贡献1864条经验 获得超6个赞
在 Go 中,你想做一个转换。
转换是形式为
T(x)whereTis a type andxis an expression that can convert to type 的表达式T。Conversion = Type "(" Expression ")" .在以下任何一种情况下,非常量值
x都可以转换为类型T:
x可分配给T。
x的类型并T具有相同的基础类型。
x的类型并且T是未命名的指针类型,并且它们的指针基类型具有相同的底层类型。
x的类型并且T都是整数或浮点类型。
x的类型 和T都是复杂类型。
x是整数或具有类型[]byte或[]rune并且T是字符串类型。
x是一个字符串并且T是[]byte或[]rune。
你要转换x,类型int,int32或int64以T类型rune,类型的别名int32。x的类型并且T都是整数类型。
因此,对于您的示例,T(x)允许并写入.rune(x)c = rune(i)
- 2 回答
- 0 关注
- 1104 浏览
添加回答
举报
