自定义命名类型的转换函数或语言构造

Are custom named type's conversions functions or language construct

我找不到此功能的任何特定文档(我在一些代码示例中看到了)。

type Event string
type Num int

func main() {
    foo := Event("my event")
    bar := Num(45)
}

Go 是否为每个自定义命名类型动态创建自定义命名函数(转换函数?)?

Is Go dynamically creating custom-named functions (conversion functions?)

不是,是语言规范定义的转换表达式。

  • Event是一个defined type,定义为type Event string
  • "my event" 是字符串文字
  • 语法 <type>(<expression>)type conversion.

An explicit conversion is an expression of the form T(x) where T is a type and x is an expression that can be converted to type T.

文字是一个常量表达式(只有一个操作数)。

A constant value x can be converted to type T if x is representable by a value of T.

字符串文字 "my event" 是否可以用 Event 值表示?是的,因为 Event 的基础类型是 string.