# 匿名结构体

匿名结构体是指没有类型名字的结构体。

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
	"fmt"
)

func main() {
	data := struct {
		Number int
		Text   string
	}{
		42,
		"Hello world!",
	}

	fmt.Printf("data: %+v\n", data)
}
```

{% endtab %}

{% tab title="Output" %}

```
data: {Number:42 Text:Hello world!}
```

{% endtab %}
{% endtabs %}

:point\_right: [点击此处](https://play.golang.org/p/GGXFAwgL2pN) :point\_left: 在编程操场亲自试运行代码。

因此，您也只能在匿名结构体定义的地方初始化它各字段的值。

匿名结构体在单元测试中很常用。

{% hint style="info" %}

#### 译者注

每个结构体都是我们自定义的一种数据类型(type)，匿名是指 type 没有名字，而不是指结构体实例化以后的 variable 没有名字。
{% endhint %}
