> For the complete documentation index, see [llms.txt](https://gocompiler.shizhz.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gocompiler.shizhz.me/golang-bian-yi-qi-lei-xing-jian-cha/4.4.47-named-lei-xing-fl.md).

# 4.4.4-7 Named 类型

Named 结构用来表示 Defined Types, 即通过语法结构`type A β`或者`type A = β`定义的类型，其中 β 表示任何合法的表达式，例如如果 β 表示`struct {...}`, 则整个声明就是一个 struct 结构体。

{% hint style="info" %}
`type A = β`叫着类型别名，是 go 1.9 引入的语法，通常情况下这种用法中 β 都是一个具体的类型名字，假设为 B，则此时 A 与 B 完全是一样的类型，纯粹只是为类型 B 引入一个别名。可以参考[Russ Cos 的文章](https://talks.golang.org/2016/refactor.article)了解使用场景。

而`type A β`则会创建一个新的类型，A 的 underlying type 是 β 所表示的类型, 例如如果 β 表示`struct {...}` , 则其类型就是上文提到的 Struct 类型；如果 β 是一个具体的类型名字，例如 C, 那么 A 的 underlying type 就是 C. 后面有专门的章节讨论Underlying Type.
{% endhint %}

Named结构定义如下：

```go
type Named struct {
	check      *Checker // for Named.under implementation
	info       typeInfo
	obj        *TypeName   // corresponding declared object
	orig       Type        // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
	underlying Type        // possibly a *Named during setup; never a *Named once set up completely
	tparams    []*TypeName // 类型参数名字，用于泛型
	targs      []Type      // 类型参数的限定类型（constraint）
	methods    []*Func     // 与该类型绑定的方法
}
```

check 是类型检查器的数据结构，详细介绍在[Checker](/golang-bian-yi-qi-lei-xing-jian-cha/4.4.5-lei-xing-jian-cha-qi.md#3-checker), 字段`info`在后期用于类型的[循环依赖检查](/golang-bian-yi-qi-lei-xing-jian-cha/4.5.31.3e-chu-li-delayed-dui-lie.md#xun-huan-yi-lai-jian-cha). 每种类型都必须实现`Underlying() Type`方法（见[Type 接口](/golang-bian-yi-qi-lei-xing-jian-cha/4.4.42-lei-xing-shu-ju-jie-gou-jie-kou.md)），其他所有类型的该方法都是返回自身，只有 Named 类型是返回属性`underlying`.
