Standard library / go/types

go/types.Scope.LookupParent()

Searches a scope and its enclosing scopes, optionally respecting declaration position. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object)

Parameters & Attributes

NameTypeDescription
namestringName of the symbol or package.
postoken.PosSource position in the associated file set.

Return Value

(*Scope, Object)

Searches a scope and its enclosing scopes, optionally respecting declaration position.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/token"  "go/types")
func main() {  p := types.NewScope(nil, 1, 20, "p")  v := types.NewVar(2, nil, "x", types.Typ[types.Int])  p.Insert(v)  s := types.NewScope(p, 5, 10, "child")  scope, obj := s.LookupParent("x", token.NoPos)  fmt.Println(scope == p, obj == v)}
Go Reference