> For the complete documentation index, see [llms.txt](https://denglj.gitbook.io/essential-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://denglj.gitbook.io/essential-go/02-basic-types/lei-xing-bie-ming.md).

# 类型别名

Go v1.9 引入了类型别名之后，使代码重构变得更轻松。

设想您的`foo`包中有可导出类型`Bar`。您想把类型`Bar`重命名为`NewBar`。

当您重命名类型时，如果没有类型别名，您必须同时将所有包里凡是用到`foo.Bar`的地方都改为`foo.NewBar`。

利用类型别名，您可以将这个过程分为2个步骤。

第一步，您可以先为`foo.Bar`引入一个别名，然后用别名去替换所有用到`foo.Bar`的地方。

```go
import "foo"
type Bar = foo.Bar // 现在Bar就是foo.Bar的别名了
```

第二步，修改`foo.Bar`为`foo.NewBar`，然后更新别名。

```go
import "foo"
// 
type Bar = foo.NewBar
```

这个改变就小多了。

现在，您就可以逐步摆脱别名，并直接使用`foo.NewBar` 类型。

这个过程听起来很麻烦，但在大型代码库中，它可能是比一次性重命名所有代码更好的选择。

用类型别名干其他事情也很具诱惑力，但您应该抵制住诱惑。

类型别名添加了一个间接层，这会影响代码的可读性，您应该有充分的好理由时才去使用它。

要了解更多，请阅读别名[提案](https://github.com/golang/proposal/blob/master/design/18130-type-alias.md)。
