如何选择匿名字段?

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

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.

huangapple
  • 本文由 发表于 2013年11月6日 02:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/19796101.html
匿名

发表评论

匿名网友

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

确定