Rob*_*rog 8 casting type-conversion go rune
假设我有一个int64变量(或其他整数大小)表示一个有效的unicode代码点,并且我想将它转换为Go中的符文,我该怎么办?
在CI中会使用类似的类型:
c = (char) i; // 7 bit ascii only
Run Code Online (Sandbox Code Playgroud)
但是在Go中,类型断言不起作用:
c, err = rune.( i)
Run Code Online (Sandbox Code Playgroud)
建议?
cth*_*m06 16
你只是想要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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9353 次 |
最近记录: |