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
| Name | Type | Description |
|---|---|---|
| T | Type | Destination type or interface, or type to inspect. |
| addressable | bool | Whether the value is addressable. |
| pkg | *Package | Package containing the symbol; nil for package-independent objects. |
| name | string | Name 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)}