Standard library / go/types

go/types.LookupFieldOrMethod()

Looks up a field or method, returning the object, selection index path and indirection flag. Expected stdout: 'X [0] false\n'.

Formal Syntax & Structure

go syntaxgo
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool)

Parameters & Attributes

NameTypeDescription
TTypeDestination type or interface, or type to inspect.
addressableboolWhether the value is addressable.
pkg*PackagePackage containing the symbol; nil for package-independent objects.
namestringName of the symbol or package.

Return Value

(obj Object, index []int, indirect bool)

Looks up a field or method, returning the object, selection index path and indirection flag.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/types")
func main() {  s := types.NewStruct([]*types.Var{types.NewField(0, nil, "X", types.Typ[types.Int], false)}, nil)  obj, idx, ind := types.LookupFieldOrMethod(s, false, nil, "X")  fmt.Println(obj.Name(), idx, ind)}
Go Reference