如何区分类型切换中的符文和int32值?

use*_*610 1 int32 go rune type-switch

拥有以下代码

var v interface{}
v = rune(1)

switch v.(type) {
    case int32:
        fmt.Println("int32")
    case rune:
        fmt.Println("rune")
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误

tmp/sandbox193184648/main.go:14: duplicate case rune in type switch
    previous case at tmp/sandbox193184648/main.go:12
Run Code Online (Sandbox Code Playgroud)

如果我用我自己的类型换行符文,那么type-switch会编译并运行

type myrune rune

var v interface{}
v = myrune(1)

switch v.(type) {
case int32:
    fmt.Println("int32")
case myrune:
    fmt.Println("rune")
}
Run Code Online (Sandbox Code Playgroud)

请参阅https://play.golang.org/p/2lMRlpCLzX

这是为什么?如何在类型切换中区分符文和int32?

Ken*_*ant 5

它是int32的别名,显然你无法区分它们.如果你真的需要,定义你自己的类型来包装其中一个将是要走的路,为什么你需要这样做?

不,你无法区分它们.rune是int32的别名,byte是uint8的别名.

https://groups.google.com/forum/m/#!searchin/golang-nuts/Rune/golang-nuts/jbWUrsQ-Uws