返回接收者本身的方法的目的是什么(Go语言)?

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

What is the purpose of a method that returns the receiver itself (Go)?

问题

这个在pkg go/token中的函数让我想知道为什么我们需要一个返回接收器本身的方法。

// Token源位置由Position值表示。
// 如果行号大于0,则Position是有效的。
//
type Position struct {
Filename string; // 文件名,如果有的话
Offset int; // 字节偏移量,从0开始
Line int; // 行号,从1开始
Column int; // 列号,从1开始(字符计数)
}

// Pos是匿名Position字段的访问器方法。
// 它返回它的接收器。
//
func (pos *Position) Pos() Position { return *pos }

英文:

This function in pkg go/token makes me wonder why we need a method that returns the receiver itself.

// Token source positions are represented by a Position value.
// A Position is valid if the line number is > 0.
//
type Position struct {
    Filename string; // filename, if any
    Offset   int;    // byte offset, starting at 0
    Line     int;    // line number, starting at 1
    Column   int;    // column number, starting at 1 (character count)
}


// Pos is an accessor method for anonymous Position fields.
// It returns its receiver.
//
func (pos *Position) Pos() Position { return *pos }

答案1

得分: 7

在这样的结构体中(来自pkg/go/ast/ast.go),下面的token.Position是一个结构体字段,但它没有任何名称:

// Comments

// A Comment node represents a single //-style or /*-style comment.
type Comment struct {
    token.Position;         // comment的起始位置
    Text            []byte; // comment文本(对于//-style的comment,不包括'\n')
}

那么,当它没有名称时,如何访问它呢?这就是.Pos()的作用。给定一个Comment节点,你可以使用.Pos方法来获取它的token.Position

 comment_position := comment_node.Pos ();

这里,comment_position现在包含了未命名("anonymous")结构体字段token.Position的内容。

英文:

In a struct like this (from pkg/go/ast/ast.go), the token.Position below is a struct field but it doesn't have any name:

// Comments

// A Comment node represents a single //-style or /*-style comment.
type Comment struct {
    token.Position;         // beginning position of the comment
    Text            []byte; // comment text (excluding '\n' for //-style comments)
}

Thus, how do you access it, when it doesn't have a name? That is what .Pos() does. Given a Comment node, you can get its token.Position by using the .Pos method on it:

 comment_position := comment_node.Pos ();

Here comment_position now contains the contents of the un-named ("anonymous") struct field token.Position.

答案2

得分: 6

这是用于使用<a href="http://golang.org/doc/go_spec.html#Struct_types">匿名字段</a>来“子类化”Position的情况:

> 声明了一个类型但没有显式字段名的字段是一个匿名字段。这样的字段类型必须指定为类型名T或类型名*T的指针,并且T本身不能是指针类型。未限定的类型名充当字段名。

因此,如果您以这种方式子类化Position,调用者可能希望能够访问“父”Position结构(例如:如果您想在位置本身上调用<code>String()</code>,而不是子类型)。<code>Pos()</code>返回它。

英文:

This is for the case where you are using <a href="http://golang.org/doc/go_spec.html#Struct_types">anonymous fields</a> to "subclass" the Position:

> A field declared with a type but no explicit field name is an anonymous field. Such a field type must be specified as a type name T or as a pointer to a type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

So if you subclass Position in this way, it may be desirable for callers to be able to access the "parent" Position structure (ex: if you want to call <code>String()</code> on the position itself, as opposed to the subtype). <code>Pos()</code> returns it.

huangapple
  • 本文由 发表于 2009年11月22日 00:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/1775907.html
匿名

发表评论

匿名网友

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

确定