pkg go/token中的这个函数让我想知道为什么我们需要一个返回接收器本身的方法.
// 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 }
Run Code Online (Sandbox Code Playgroud)
小智 5
在这样的结构中(来自pkg/go/ast/ast.go),token.Position下面是一个struct字段,但它没有任何名称:
// 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)
}
Run Code Online (Sandbox Code Playgroud)
因此,当它没有名称时,你如何访问它?这是做什么的.Pos().给定一个Comment节点,你可以token.Position通过使用.Pos它的方法得到它:
comment_position := comment_node.Pos ();
Run Code Online (Sandbox Code Playgroud)
这里comment_position现在包含未命名("匿名")结构字段的内容token.Position.