这个 Golang 结构体中的第二对大括号是用来定义结构体的字段的。

huangapple go评论80阅读模式
英文:

What are the second pair of braces in this Golang struct?

问题

这看起来是一个带有嵌入字段 sync.Mutex 的结构体,但我对第二组大括号有些困惑。它可以编译和执行,但是它的作用是什么?为什么 make 指令上的标签很重要(它确实重要),以及逗号的作用是什么?谢谢...

英文:
var cache = struct    {
    sync.Mutex
    mapping map[string]string
} {
    mapping: make(map[string]string),
}

This looks like a struct with an embedded field sync.Mutex but I can't get my head around the second set of braces. It compiles and executes but what's up? Why does the label on the make instruction matter (it does) and the comma? Thanks...

答案1

得分: 12

你所提供的示例等同于:

type Cache struct {
    sync.Mutex
    mapping map[string]string
}

cache := Cache{
    mapping: make(map[string]string),
}

不同之处在于你的示例中没有声明Cache类型,而是使用了一个匿名结构体。在你的示例中,与我的Cache类型相反,类型是整个结构体:

struct {
    sync.Mutex
    mapping map[string]string
}

所以可以将第二对大括号视为:

cache := Cache{
    mapping: make(map[string]string),
}

make是一个内置函数,类似于C语言中的calloc(),它们都用于初始化一个填充了零值的数据结构。在Go中,某些数据结构需要以这种方式进行初始化,而其他数据结构(大部分是结构体)会自动初始化为零值。这里需要使用该字段,以便编译器知道cache.mapping是一个空的map[string]string

逗号是Go语言的格式要求之一。你可以将Cache{mapping: make(map[string]string)}写在一行上,但是一旦字段的赋值与大括号的开头和结尾不在同一行上,就需要加上逗号。

英文:

The example you have is equivalent to:

type Cache struct {
    sync.Mutex
    mapping map[string]string
}

cache := Cache{
    mapping: make(map[string]string),
}

Except in your example you do not declare a type of Cache and instead have an anonymous struct. In your example, as oppose to my Cache type, the type is the entire

struct {
    sync.Mutex
    mapping map[string]string
}

So think of the second pair of braces as the

cache := Cache{
    mapping: make(map[string]string),
}

part.

make is a built in function that works similarly to C's calloc() which both initialize a data structure filled with 0'd values, in Go's case, certain data structures need to be initialized this way, other's (for the most part structs) are initialized with 0'd values automatically. The field there is needed so that the compiler now's cache.mapping is a empty map[string]string.

The comma there is part of Go's formatting, you can do Cache{mapping: make(map[string]string)} all on one line, but the moment the field's assignment is on a different line than the opening and closing braces, it requires a comma.

答案2

得分: 0

这被称为“结构体字面量”或“匿名结构体”,实际上,在Go语言中,这是创建结构体的常用方式,只是可能不太明显,因为你可能习惯了为结构体类型创建新类型,以使声明更简洁。

在Go语言中,整个结构体定义实际上是一种类型,就像int[]bytestring一样。就像你可以这样做:

type NewType int
var a NewType = 5 // a 是 NewType 类型(基于 int)

或者:

a := 5 // a 是 int 类型

它们都是看起来像int的不同类型,你也可以对结构体做同样的事情:

// a 是 NewType 类型(struct{} 类型)。
type NewType struct{
  A string
}
a := NewType{
  A: "test string",
}

// a 是 struct{A string} 类型
a := struct{
  A string
}{
  A: "test string",
}

类型名称(NewType)只是被结构体本身的类型(struct{A string})所取代。请注意,它们在比较或赋值时不是相同的类型(别名),但它们具有相同的语义。

英文:

This is called a "struct literal" or an "anonymous struct" and is, in fact, how you always create structs in Go, it just may not be immediately obvious since you might be used to creating new types for struct types to make declaring them a bit less verbose.

An entire struct definition is actually a type in Go, just like int or []byte or string. Just as you can do:

type NewType int
var a NewType = 5 // a is a NewType (which is based on an int)

or:

a := 5 // a is an int

and both are distinct types that look like ints, you can also do the same thing with structs:

// a is type NewType (which is a struct{}).
type NewType struct{
  A string
}
a := NewType{
  A: "test string",
}

// a is type struct{A string}
a := struct{
  A string
}{
  A: "test string",
}

the type name (NewType) has just been replaced with the type of the struct itself, struct{A string}. Note that they are not the same type (an alias) for the purpose of comparison or assignment, but they do share the same semantics.

huangapple
  • 本文由 发表于 2016年11月2日 07:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/40370006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定