type关键字 结构体名字 struct{}
type Person struct {
name string
sex ,age int
当我们创建结构体时,字段可以只有类型,而没有字段名。这样的字段称为匿名字段(Anonymous Field)。
以下代码创建一个 Person
结构体,它含有两个匿名字段 string
和 int
。
type Person struct {
string
int
}
type Person struct {
name string
sex ,age int
hobby Hobby
}
type Hobby struct {
id int
name string
}
package main import "fmt" func main(){ a:= struct { name string age int }{"xy",20} fmt.Println(a) }
原文:https://www.cnblogs.com/xiongying4/p/12025343.html