package token

Import Path
	go/token (on go.dev)

Dependency Relation
	imports 8 packages, and imported by 17 packages

Involved Source Files position.go serialize.go Package token defines constants representing the lexical tokens of the Go programming language and basic operations on tokens (printing, predicates).
Code Examples package main import ( "fmt" "go/ast" "go/parser" "go/token" ) func main() { fset := token.NewFileSet() const src = `package main import "fmt" import "go/token" //line :1:5 type p = token.Pos const bad = token.NoPos //line fake.go:42:11 func ok(pos p) bool { return pos != bad } /*line :7:9*/func main() { fmt.Println(ok(bad) == bad.IsValid()) } ` f, err := parser.ParseFile(fset, "main.go", src, 0) if err != nil { fmt.Println(err) return } // Print the location and kind of each declaration in f. for _, decl := range f.Decls { // Get the filename, line, and column back via the file set. // We get both the relative and absolute position. // The relative position is relative to the last line directive. // The absolute position is the exact position in the source. pos := decl.Pos() relPosition := fset.Position(pos) absPosition := fset.PositionFor(pos, false) // Either a FuncDecl or GenDecl, since we exit on error. kind := "func" if gen, ok := decl.(*ast.GenDecl); ok { kind = gen.Tok.String() } // If the relative and absolute positions differ, show both. fmtPosition := relPosition.String() if relPosition != absPosition { fmtPosition += "[" + absPosition.String() + "]" } fmt.Printf("%s: %s\n", fmtPosition, kind) } }
Package-Level Type Names (total 5)
/* sort by: | */
A File is a handle for a file belonging to a [FileSet]. A File has a name, size, and line offset table. AddLine adds the line offset for a new line. The line offset must be larger than the offset for the previous line and smaller than the file size; otherwise the line offset is ignored. AddLineColumnInfo adds alternative file, line, and column number information for a given file offset. The offset must be larger than the offset for the previously added alternative line info and smaller than the file size; otherwise the information is ignored. AddLineColumnInfo is typically used to register alternative position information for line directives such as //line filename:line:column. AddLineInfo is like [File.AddLineColumnInfo] with a column = 1 argument. It is here for backward-compatibility for code prior to Go 1.11. Base returns the base offset of file f as registered with AddFile. Line returns the line number for the given file position p; p must be a [Pos] value in that file or [NoPos]. LineCount returns the number of lines in file f. LineStart returns the [Pos] value of the start of the specified line. It ignores any alternative positions set using [File.AddLineColumnInfo]. LineStart panics if the 1-based line number is invalid. Lines returns the effective line offset table of the form described by [File.SetLines]. Callers must not mutate the result. MergeLine merges a line with the following line. It is akin to replacing the newline character at the end of the line with a space (to not change the remaining offsets). To obtain the line number, consult e.g. [Position.Line]. MergeLine will panic if given an invalid line number. Name returns the file name of file f as registered with AddFile. Offset returns the offset for the given file position p. If p is before the file's start position (or if p is NoPos), the result is 0; if p is past the file's end position, the the result is the file size (see also go.dev/issue/57490). The following invariant, though not true for offset values in general, holds for the result offset: f.Offset(f.Pos(offset)) == offset Pos returns the Pos value for the given file offset. If offset is negative, the result is the file's start position; if the offset is too large, the result is the file's end position (see also go.dev/issue/57490). The following invariant, though not true for Pos values in general, holds for the result p: f.Pos(f.Offset(p)) == p. Position returns the Position value for the given file position p. If p is out of bounds, it is adjusted to match the File.Offset behavior. Calling f.Position(p) is equivalent to calling f.PositionFor(p, true). PositionFor returns the Position value for the given file position p. If p is out of bounds, it is adjusted to match the File.Offset behavior. If adjusted is set, the position may be adjusted by position-altering //line comments; otherwise those comments are ignored. p must be a Pos value in f or NoPos. SetLines sets the line offsets for a file and reports whether it succeeded. The line offsets are the offsets of the first character of each line; for instance for the content "ab\nc\n" the line offsets are {0, 3}. An empty file has an empty line offset table. Each line offset must be larger than the offset for the previous line and smaller than the file size; otherwise SetLines fails and returns false. Callers must not mutate the provided slice after SetLines returns. SetLinesForContent sets the line offsets for the given file content. It ignores position-altering //line comments. Size returns the size of file f as registered with AddFile. func (*FileSet).AddFile(filename string, base, size int) *File func (*FileSet).File(p Pos) (f *File) func (*FileSet).RemoveFile(file *File) func go/scanner.(*Scanner).Init(file *File, src []byte, err scanner.ErrorHandler, mode scanner.Mode)
A FileSet represents a set of source files. Methods of file sets are synchronized; multiple goroutines may invoke them concurrently. The byte offsets for each file in a file set are mapped into distinct (integer) intervals, one interval [base, base+size] per file. [FileSet.Base] represents the first byte in the file, and size is the corresponding file size. A [Pos] value is a value in such an interval. By determining the interval a [Pos] value belongs to, the file, its file base, and thus the byte offset (position) the [Pos] value is representing can be computed. When adding a new file, a file base must be provided. That can be any integer value that is past the end of any interval of any file already in the file set. For convenience, [FileSet.Base] provides such a value, which is simply the end of the Pos interval of the most recently added file, plus one. Unless there is a need to extend an interval later, using the [FileSet.Base] should be used as argument for [FileSet.AddFile]. A [File] may be removed from a FileSet when it is no longer needed. This may reduce memory usage in a long-running application. AddFile adds a new file with a given filename, base offset, and file size to the file set s and returns the file. Multiple files may have the same name. The base offset must not be smaller than the [FileSet.Base], and size must not be negative. As a special case, if a negative base is provided, the current value of the [FileSet.Base] is used instead. Adding the file will set the file set's [FileSet.Base] value to base + size + 1 as the minimum base value for the next file. The following relationship exists between a [Pos] value p for a given file offset offs: int(p) = base + offs with offs in the range [0, size] and thus p in the range [base, base+size]. For convenience, [File.Pos] may be used to create file-specific position values from a file offset. Base returns the minimum base offset that must be provided to [FileSet.AddFile] when adding the next file. File returns the file that contains the position p. If no such file is found (for instance for p == [NoPos]), the result is nil. Iterate calls f for the files in the file set in the order they were added until f returns false. Position converts a [Pos] p in the fileset into a Position value. Calling s.Position(p) is equivalent to calling s.PositionFor(p, true). PositionFor converts a [Pos] p in the fileset into a [Position] value. If adjusted is set, the position may be adjusted by position-altering //line comments; otherwise those comments are ignored. p must be a [Pos] value in s or [NoPos]. Read calls decode to deserialize a file set into s; s must not be nil. RemoveFile removes a file from the [FileSet] so that subsequent queries for its [Pos] interval yield a negative result. This reduces the memory usage of a long-lived [FileSet] that encounters an unbounded stream of files. Removing a file that does not belong to the set has no effect. Write calls encode to serialize the file set s. func NewFileSet() *FileSet func go/ast.Fprint(w io.Writer, fset *FileSet, x any, f ast.FieldFilter) error func go/ast.NewCommentMap(fset *FileSet, node ast.Node, comments []*ast.CommentGroup) ast.CommentMap func go/ast.NewPackage(fset *FileSet, files map[string]*ast.File, importer ast.Importer, universe *ast.Scope) (*ast.Package, error) func go/ast.Print(fset *FileSet, x any) error func go/ast.SortImports(fset *FileSet, f *ast.File) func go/doc.NewFromFiles(fset *FileSet, files []*ast.File, importPath string, opts ...any) (*doc.Package, error) func go/format.Node(dst io.Writer, fset *FileSet, node any) error func go/importer.ForCompiler(fset *FileSet, compiler string, lookup importer.Lookup) types.Importer func go/internal/gcimporter.Import(fset *FileSet, packages map[string]*types.Package, path, srcDir string, lookup func(path string) (io.ReadCloser, error)) (pkg *types.Package, err error) func go/internal/srcimporter.New(ctxt *build.Context, fset *FileSet, packages map[string]*types.Package) *srcimporter.Importer func go/parser.ParseDir(fset *FileSet, path string, filter func(fs.FileInfo) bool, mode parser.Mode) (pkgs map[string]*ast.Package, first error) func go/parser.ParseExprFrom(fset *FileSet, filename string, src any, mode parser.Mode) (expr ast.Expr, err error) func go/parser.ParseFile(fset *FileSet, filename string, src any, mode parser.Mode) (f *ast.File, err error) func go/printer.Fprint(output io.Writer, fset *FileSet, node any) error func go/printer.(*Config).Fprint(output io.Writer, fset *FileSet, node any) error func go/types.CheckExpr(fset *FileSet, pkg *types.Package, pos Pos, expr ast.Expr, info *types.Info) (err error) func go/types.Eval(fset *FileSet, pkg *types.Package, pos Pos, expr string) (_ types.TypeAndValue, err error) func go/types.NewChecker(conf *types.Config, fset *FileSet, pkg *types.Package, info *types.Info) *types.Checker func go/types.(*Config).Check(path string, fset *FileSet, files []*ast.File, info *types.Info) (*types.Package, error)
Pos is a compact encoding of a source position within a file set. It can be converted into a [Position] for a more convenient, but much larger, representation. The Pos value for a given file is a number in the range [base, base+size], where base and size are specified when a file is added to the file set. The difference between a Pos value and the corresponding file base corresponds to the byte offset of that position (represented by the Pos value) from the beginning of the file. Thus, the file base offset is the Pos value representing the first byte in the file. To create the Pos value for a specific source offset (measured in bytes), first add the respective file to the current file set using [FileSet.AddFile] and then call [File.Pos](offset) for that file. Given a Pos value p for a specific file set fset, the corresponding [Position] value is obtained by calling fset.Position(p). Pos values can be compared directly with the usual comparison operators: If two Pos values p and q are in the same file, comparing p and q is equivalent to comparing the respective source file offsets. If p and q are in different files, p < q is true if the file implied by p was added to the respective file set before the file implied by q. IsValid reports whether the position is valid. Pos : database/sql/driver.Validator func (*File).LineStart(line int) Pos func (*File).Pos(offset int) Pos func go/ast.(*ArrayType).End() Pos func go/ast.(*ArrayType).Pos() Pos func go/ast.(*AssignStmt).End() Pos func go/ast.(*AssignStmt).Pos() Pos func go/ast.(*BadDecl).End() Pos func go/ast.(*BadDecl).Pos() Pos func go/ast.(*BadExpr).End() Pos func go/ast.(*BadExpr).Pos() Pos func go/ast.(*BadStmt).End() Pos func go/ast.(*BadStmt).Pos() Pos func go/ast.(*BasicLit).End() Pos func go/ast.(*BasicLit).Pos() Pos func go/ast.(*BinaryExpr).End() Pos func go/ast.(*BinaryExpr).Pos() Pos func go/ast.(*BlockStmt).End() Pos func go/ast.(*BlockStmt).Pos() Pos func go/ast.(*BranchStmt).End() Pos func go/ast.(*BranchStmt).Pos() Pos func go/ast.(*CallExpr).End() Pos func go/ast.(*CallExpr).Pos() Pos func go/ast.(*CaseClause).End() Pos func go/ast.(*CaseClause).Pos() Pos func go/ast.(*ChanType).End() Pos func go/ast.(*ChanType).Pos() Pos func go/ast.(*CommClause).End() Pos func go/ast.(*CommClause).Pos() Pos func go/ast.(*Comment).End() Pos func go/ast.(*Comment).Pos() Pos func go/ast.(*CommentGroup).End() Pos func go/ast.(*CommentGroup).Pos() Pos func go/ast.(*CompositeLit).End() Pos func go/ast.(*CompositeLit).Pos() Pos func go/ast.Decl.End() Pos func go/ast.Decl.Pos() Pos func go/ast.(*DeclStmt).End() Pos func go/ast.(*DeclStmt).Pos() Pos func go/ast.(*DeferStmt).End() Pos func go/ast.(*DeferStmt).Pos() Pos func go/ast.(*Ellipsis).End() Pos func go/ast.(*Ellipsis).Pos() Pos func go/ast.(*EmptyStmt).End() Pos func go/ast.(*EmptyStmt).Pos() Pos func go/ast.Expr.End() Pos func go/ast.Expr.Pos() Pos func go/ast.(*ExprStmt).End() Pos func go/ast.(*ExprStmt).Pos() Pos func go/ast.(*Field).End() Pos func go/ast.(*Field).Pos() Pos func go/ast.(*FieldList).End() Pos func go/ast.(*FieldList).Pos() Pos func go/ast.(*File).End() Pos func go/ast.(*File).Pos() Pos func go/ast.(*ForStmt).End() Pos func go/ast.(*ForStmt).Pos() Pos func go/ast.(*FuncDecl).End() Pos func go/ast.(*FuncDecl).Pos() Pos func go/ast.(*FuncLit).End() Pos func go/ast.(*FuncLit).Pos() Pos func go/ast.(*FuncType).End() Pos func go/ast.(*FuncType).Pos() Pos func go/ast.(*GenDecl).End() Pos func go/ast.(*GenDecl).Pos() Pos func go/ast.(*GoStmt).End() Pos func go/ast.(*GoStmt).Pos() Pos func go/ast.(*Ident).End() Pos func go/ast.(*Ident).Pos() Pos func go/ast.(*IfStmt).End() Pos func go/ast.(*IfStmt).Pos() Pos func go/ast.(*ImportSpec).End() Pos func go/ast.(*ImportSpec).Pos() Pos func go/ast.(*IncDecStmt).End() Pos func go/ast.(*IncDecStmt).Pos() Pos func go/ast.(*IndexExpr).End() Pos func go/ast.(*IndexExpr).Pos() Pos func go/ast.(*IndexListExpr).End() Pos func go/ast.(*IndexListExpr).Pos() Pos func go/ast.(*InterfaceType).End() Pos func go/ast.(*InterfaceType).Pos() Pos func go/ast.(*KeyValueExpr).End() Pos func go/ast.(*KeyValueExpr).Pos() Pos func go/ast.(*LabeledStmt).End() Pos func go/ast.(*LabeledStmt).Pos() Pos func go/ast.(*MapType).End() Pos func go/ast.(*MapType).Pos() Pos func go/ast.Node.End() Pos func go/ast.Node.Pos() Pos func go/ast.(*Object).Pos() Pos func go/ast.(*Package).End() Pos func go/ast.(*Package).Pos() Pos func go/ast.(*ParenExpr).End() Pos func go/ast.(*ParenExpr).Pos() Pos func go/ast.(*RangeStmt).End() Pos func go/ast.(*RangeStmt).Pos() Pos func go/ast.(*ReturnStmt).End() Pos func go/ast.(*ReturnStmt).Pos() Pos func go/ast.(*SelectorExpr).End() Pos func go/ast.(*SelectorExpr).Pos() Pos func go/ast.(*SelectStmt).End() Pos func go/ast.(*SelectStmt).Pos() Pos func go/ast.(*SendStmt).End() Pos func go/ast.(*SendStmt).Pos() Pos func go/ast.(*SliceExpr).End() Pos func go/ast.(*SliceExpr).Pos() Pos func go/ast.Spec.End() Pos func go/ast.Spec.Pos() Pos func go/ast.(*StarExpr).End() Pos func go/ast.(*StarExpr).Pos() Pos func go/ast.Stmt.End() Pos func go/ast.Stmt.Pos() Pos func go/ast.(*StructType).End() Pos func go/ast.(*StructType).Pos() Pos func go/ast.(*SwitchStmt).End() Pos func go/ast.(*SwitchStmt).Pos() Pos func go/ast.(*TypeAssertExpr).End() Pos func go/ast.(*TypeAssertExpr).Pos() Pos func go/ast.(*TypeSpec).End() Pos func go/ast.(*TypeSpec).Pos() Pos func go/ast.(*TypeSwitchStmt).End() Pos func go/ast.(*TypeSwitchStmt).Pos() Pos func go/ast.(*UnaryExpr).End() Pos func go/ast.(*UnaryExpr).Pos() Pos func go/ast.(*ValueSpec).End() Pos func go/ast.(*ValueSpec).Pos() Pos func go/internal/typeparams.(*IndexExpr).Pos() Pos func go/scanner.(*Scanner).Scan() (pos Pos, tok Token, lit string) func go/types.Object.Pos() Pos func go/types.(*Scope).End() Pos func go/types.(*Scope).Pos() Pos func (*File).Line(p Pos) int func (*File).Offset(p Pos) int func (*File).Position(p Pos) (pos Position) func (*File).PositionFor(p Pos, adjusted bool) (pos Position) func (*FileSet).File(p Pos) (f *File) func (*FileSet).Position(p Pos) (pos Position) func (*FileSet).PositionFor(p Pos, adjusted bool) (pos Position) func go/internal/typeparams.PackIndexExpr(x ast.Expr, lbrack Pos, exprs []ast.Expr, rbrack Pos) ast.Expr func go/internal/typeparams.PackIndexExpr(x ast.Expr, lbrack Pos, exprs []ast.Expr, rbrack Pos) ast.Expr func go/types.CheckExpr(fset *FileSet, pkg *types.Package, pos Pos, expr ast.Expr, info *types.Info) (err error) func go/types.Eval(fset *FileSet, pkg *types.Package, pos Pos, expr string) (_ types.TypeAndValue, err error) func go/types.NewConst(pos Pos, pkg *types.Package, name string, typ types.Type, val constant.Value) *types.Const func go/types.NewField(pos Pos, pkg *types.Package, name string, typ types.Type, embedded bool) *types.Var func go/types.NewFunc(pos Pos, pkg *types.Package, name string, sig *types.Signature) *types.Func func go/types.NewLabel(pos Pos, pkg *types.Package, name string) *types.Label func go/types.NewParam(pos Pos, pkg *types.Package, name string, typ types.Type) *types.Var func go/types.NewPkgName(pos Pos, pkg *types.Package, name string, imported *types.Package) *types.PkgName func go/types.NewScope(parent *types.Scope, pos, end Pos, comment string) *types.Scope func go/types.NewTypeName(pos Pos, pkg *types.Package, name string, typ types.Type) *types.TypeName func go/types.NewVar(pos Pos, pkg *types.Package, name string, typ types.Type) *types.Var func go/types.(*Scope).Contains(pos Pos) bool func go/types.(*Scope).Innermost(pos Pos) *types.Scope func go/types.(*Scope).LookupParent(name string, pos Pos) (*types.Scope, types.Object) const NoPos
Position describes an arbitrary source position including the file, line, and column location. A Position is valid if the line number is > 0. // column number, starting at 1 (character count per line) // filename, if any // line number, starting at 1 // byte offset, starting at 0 IsValid reports whether the position is valid. String returns a string in one of several forms: file:line:column valid position with file name file:line valid position with file name but no column (column == 0) line:column valid position without file name line valid position without file name and no column (column == 0) file invalid position with file name - invalid position without file name *Position : database/sql/driver.Validator Position : expvar.Var Position : fmt.Stringer func (*File).Position(p Pos) (pos Position) func (*File).PositionFor(p Pos, adjusted bool) (pos Position) func (*FileSet).Position(p Pos) (pos Position) func (*FileSet).PositionFor(p Pos, adjusted bool) (pos Position) func go/scanner.(*ErrorList).Add(pos Position, msg string)
Token is the set of lexical tokens of the Go programming language. IsKeyword returns true for tokens corresponding to keywords; it returns false otherwise. IsLiteral returns true for tokens corresponding to identifiers and basic type literals; it returns false otherwise. IsOperator returns true for tokens corresponding to operators and delimiters; it returns false otherwise. Precedence returns the operator precedence of the binary operator op. If op is not a binary operator, the result is LowestPrecedence. String returns the string corresponding to the token tok. For operators, delimiters, and keywords the string is the actual token character sequence (e.g., for the token [ADD], the string is "+"). For all other tokens the string corresponds to the token constant name (e.g. for the token [IDENT], the string is "IDENT"). Token : expvar.Var Token : fmt.Stringer func Lookup(ident string) Token func go/scanner.(*Scanner).Scan() (pos Pos, tok Token, lit string) func go/constant.BinaryOp(x_ constant.Value, op Token, y_ constant.Value) constant.Value func go/constant.Compare(x_ constant.Value, op Token, y_ constant.Value) bool func go/constant.MakeFromLiteral(lit string, tok Token, zero uint) constant.Value func go/constant.Shift(x constant.Value, op Token, s uint) constant.Value func go/constant.UnaryOp(op Token, y constant.Value, prec uint) constant.Value const ADD const ADD_ASSIGN const AND const AND_ASSIGN const AND_NOT const AND_NOT_ASSIGN const ARROW const ASSIGN const BREAK const CASE const CHAN const CHAR const COLON const COMMA const COMMENT const CONST const CONTINUE const DEC const DEFAULT const DEFER const DEFINE const ELLIPSIS const ELSE const EOF const EQL const FALLTHROUGH const FLOAT const FOR const FUNC const GEQ const GO const GOTO const GTR const IDENT const IF const ILLEGAL const IMAG const IMPORT const INC const INT const INTERFACE const LAND const LBRACE const LBRACK const LEQ const LOR const LPAREN const LSS const MAP const MUL const MUL_ASSIGN const NEQ const NOT const OR const OR_ASSIGN const PACKAGE const PERIOD const QUO const QUO_ASSIGN const RANGE const RBRACE const RBRACK const REM const REM_ASSIGN const RETURN const RPAREN const SELECT const SEMICOLON const SHL const SHL_ASSIGN const SHR const SHR_ASSIGN const STRING const STRUCT const SUB const SUB_ASSIGN const SWITCH const TILDE const TYPE const VAR const XOR const XOR_ASSIGN
Package-Level Functions (total 5)
IsExported reports whether name starts with an upper-case letter.
IsIdentifier reports whether name is a Go identifier, that is, a non-empty string made up of letters, digits, and underscores, where the first character is not a digit. Keywords are not identifiers.
IsKeyword reports whether name is a Go keyword, such as "func" or "return".
Lookup maps an identifier to its keyword token or [IDENT] (if not a keyword).
NewFileSet creates a new file set.
Package-Level Constants (total 86)
Operators and delimiters
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
Keywords
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
A set of constants for precedence-based expression parsing. Non-operators have lowest precedence, followed by operators starting with precedence 1 up to unary operators. The highest precedence serves as "catch-all" precedence for selector, indexing, and other operator and delimiter tokens.
Identifiers and basic type literals (these tokens stand for classes of literals)
The list of tokens.
Special tokens
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
A set of constants for precedence-based expression parsing. Non-operators have lowest precedence, followed by operators starting with precedence 1 up to unary operators. The highest precedence serves as "catch-all" precedence for selector, indexing, and other operator and delimiter tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The zero value for [Pos] is NoPos; there is no file and line information associated with it, and NoPos.IsValid() is false. NoPos is always smaller than any other [Pos] value. The corresponding [Position] value for NoPos is the zero value for [Position].
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
The list of tokens.
additional tokens, handled in an ad-hoc manner
The list of tokens.
A set of constants for precedence-based expression parsing. Non-operators have lowest precedence, followed by operators starting with precedence 1 up to unary operators. The highest precedence serves as "catch-all" precedence for selector, indexing, and other operator and delimiter tokens.
The list of tokens.
The list of tokens.
The list of tokens.