test(demo): add go/types Scope.Insert demo
Add a demo program to demonstrate the fix for cross-package interface private method calls. This demo uses go/types.Scope.Insert which has private methods in the go/types package, making it a good test case for the bug fix in PR #1371. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
This commit is contained in:
32
_demo/go/gotypesissue/main.go
Normal file
32
_demo/go/gotypesissue/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/token"
|
||||
"go/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pkg := types.NewPackage("example", "example")
|
||||
scope := pkg.Scope()
|
||||
|
||||
obj := types.NewVar(token.NoPos, pkg, "testVar", types.Typ[types.Int])
|
||||
|
||||
insertedObj := scope.Insert(obj)
|
||||
if insertedObj != nil {
|
||||
println("ERROR: Variable already exists in scope")
|
||||
return
|
||||
}
|
||||
|
||||
lookup := scope.Lookup("testVar")
|
||||
if lookup == nil {
|
||||
println("ERROR: Failed to lookup variable")
|
||||
return
|
||||
}
|
||||
|
||||
if lookup.Name() != "testVar" {
|
||||
println("ERROR: Wrong variable name")
|
||||
return
|
||||
}
|
||||
|
||||
println("SUCCESS: Scope.Insert and Lookup work correctly")
|
||||
}
|
||||
Reference in New Issue
Block a user