英文:
How to select anonymous field?
问题
从GoQuery中:
type Document struct {
*Selection
Url *url.URL
// 包含被过滤或未导出的字段
}
我想从*Document
变量中获取*Selection
指针:
doc, e := goquery.NewDocument("http://www.gemalto.com/companyinfo/careers/")
var sel *goquery.Selection = doc // 错误!
sel = doc.(*goquery.Selection) // 也是错误的!
英文:
From GoQuery:
type Document struct {
*Selection
Url *url.URL
// contains filtered or unexported fields
}
I want to get *Selection
pointer from a *Document
variable:
doc, e := goquery.NewDocument("http://www.gemalto.com/companyinfo/careers/")
var sel *goquery.Selection = doc // error!
sel = doc.(*goquery.Selection) // also error!
答案1
得分: 6
不合格的类型名称充当字段名称
var sel *goquery.Selection = doc.Selection
有关详细信息,请参阅结构类型部分:
声明了类型但没有显式字段名的字段是匿名字段,也称为嵌入字段或将类型嵌入结构体中的字段。嵌入类型必须指定为类型名称
T
或非接口类型名称*T
的指针,并且T
本身不能是指针类型。不合格的类型名称充当字段名称。
英文:
The unqualified type name acts as the field name
var sel *goquery.Selection = doc.Selection
See the section on Struct Types for details:
> A field declared with a type but no explicit field name is an
> anonymous field, also called an embedded field or an embedding of the
> type in the struct. An embedded type must be specified as a type name
> T
or as a pointer to a non-interface type name *T
, and T
itself may
> not be a pointer type. The unqualified type name acts as the field
> name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论