Go语言中单引号'1'表示字符常量类型为rune

ChatGPT4o 使用 ChatGPT 4o 回答

问题:

golang '1'是什么类型

回答:

在 Go 语言中,单引号 '1' 表示一个字符常量(rune)。字符常量是用单引号括起来的单个字符,表示 Unicode 码点。字符常量的类型是 rune,它是 Go 语言中的一个别名类型,实际上是 int32

例如:

package main

import (
    "fmt"
)

func main() {
    var ch rune = '1'
    fmt.Printf("字符: %c\n", ch)
    fmt.Printf("Unicode 码点: %U\n", ch)
    fmt.Printf("类型: %T\n", ch)
}

输出结果:

字符: 1
Unicode 码点: U+0031
类型: int32

在这个例子中,字符 '1' 的 Unicode 码点是 U+0031,它的类型是 rune(即 int32)。