Go 结构中的无名字段?
Nameless fields in Go structs?
package main
import "fmt"
type myType struct {
string
}
func main() {
obj := myType{"Hello World"}
fmt.Println(obj)
}
结构中无名字段的用途是什么?
是否可以像访问命名字段那样访问这些字段?
参见“Embedding in Go ": you embed an anonymous field in a struct:这通常与嵌入式结构一起使用,而不是像 string
这样的基本类型。该类型没有要公开的“提升字段”。
A field or method f
of an anonymous field in a struct x
is called promoted if x.f
is a legal selector that denotes that field or method f
.
Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.
(这里string
本身没有字段)
请参阅“”中的类型嵌入示例。
Is it possible to access these fields like you can do with named fields?
A fmt.Println(obj.string)
会 return Hello World
而不是 {Hello World}
.
没有对选择的答案不尊重,但它没有为我阐明概念。
有两件事正在发生。首先是匿名字段。第二个是 "promoted" 字段。
对于匿名字段,您可以使用的字段名称是类型的名称。第一个匿名字段是 "promoted",这意味着您在结构 "passes through" 上访问的任何字段都是提升的匿名字段。这显示了两个概念:
package main
import (
"fmt"
"time"
)
type Widget struct {
name string
}
type WrappedWidget struct {
Widget // this is the promoted field
time.Time // this is another anonymous field that has a runtime name of Time
price int64 // normal field
}
func main() {
widget := Widget{"my widget"}
wrappedWidget := WrappedWidget{widget, time.Now(), 1234}
fmt.Printf("Widget named %s, created at %s, has price %d\n",
wrappedWidget.name, // name is passed on to the wrapped Widget since it's
// the promoted field
wrappedWidget.Time, // We access the anonymous time.Time as Time
wrappedWidget.price)
fmt.Printf("Widget named %s, created at %s, has price %d\n",
wrappedWidget.Widget.name, // We can also access the Widget directly
// via Widget
wrappedWidget.Time,
wrappedWidget.price)
}
输出为:
Widget named my widget, created at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001, has price 1234
Widget named my widget, created at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001, has price 1234```
package main
import "fmt"
type myType struct {
string
}
func main() {
obj := myType{"Hello World"}
fmt.Println(obj)
}
结构中无名字段的用途是什么?
是否可以像访问命名字段那样访问这些字段?
参见“Embedding in Go ": you embed an anonymous field in a struct:这通常与嵌入式结构一起使用,而不是像 string
这样的基本类型。该类型没有要公开的“提升字段”。
A field or method
f
of an anonymous field in a structx
is called promoted ifx.f
is a legal selector that denotes that field or methodf
.Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.
(这里string
本身没有字段)
请参阅“
Is it possible to access these fields like you can do with named fields?
A fmt.Println(obj.string)
会 return Hello World
而不是 {Hello World}
.
没有对选择的答案不尊重,但它没有为我阐明概念。
有两件事正在发生。首先是匿名字段。第二个是 "promoted" 字段。
对于匿名字段,您可以使用的字段名称是类型的名称。第一个匿名字段是 "promoted",这意味着您在结构 "passes through" 上访问的任何字段都是提升的匿名字段。这显示了两个概念:
package main
import (
"fmt"
"time"
)
type Widget struct {
name string
}
type WrappedWidget struct {
Widget // this is the promoted field
time.Time // this is another anonymous field that has a runtime name of Time
price int64 // normal field
}
func main() {
widget := Widget{"my widget"}
wrappedWidget := WrappedWidget{widget, time.Now(), 1234}
fmt.Printf("Widget named %s, created at %s, has price %d\n",
wrappedWidget.name, // name is passed on to the wrapped Widget since it's
// the promoted field
wrappedWidget.Time, // We access the anonymous time.Time as Time
wrappedWidget.price)
fmt.Printf("Widget named %s, created at %s, has price %d\n",
wrappedWidget.Widget.name, // We can also access the Widget directly
// via Widget
wrappedWidget.Time,
wrappedWidget.price)
}
输出为:
Widget named my widget, created at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001, has price 1234
Widget named my widget, created at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001, has price 1234```