GoLang中的内置类型(或未定义类型)是什么?

时间:2020-08-29 17:59:24

标签: go

Spec说:

如果满足以下条件之一,则可以将值x分配给类型T的变量(“ x可以分配给T”)

x的类型V和T具有相同的基础类型,并且V或T中至少有一个不是已定义类型。


数字类型uint8defined type,因此使用上述规则,可分配性在以下代码中失败:

package main

type Point struct {
    x uint8
    y uint8
}

type Polar Point

var p Polar = Point{x: 1, y: 2}

func main() {

}

要测试以上规则,GoLang中的未定义类型是什么?

1 个答案:

答案 0 :(得分:3)

未定义类型是没有分配标识符的类型。例如:

type S struct {
  A int
}

func main() {
  var s S
  // You can assign a literal struct with the same underlying type to a variable of type S
  s=struct{A int}{1}
}

上面,struct {A int}的类型不是定义的类型。

相关问题