Source file src/cmd/cgo/internal/testplugin/testdata/issue81303/p/p.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p
     6  
     7  import (
     8  	"go/ast"
     9  	"go/parser"
    10  	"go/token"
    11  	"log"
    12  )
    13  
    14  const src = `package p
    15  
    16  import "fmt"
    17  
    18  type T struct{ x int }
    19  
    20  func (t *T) M(n int) (r int) {
    21  	defer func() { r++ }()
    22  	go fmt.Println(n)
    23  	if n > 0 {
    24  		r = n
    25  	} else {
    26  		r = -n
    27  	}
    28  	for i := 0; i < n; i++ {
    29  		r += t.x
    30  	}
    31  	switch v := any(n).(type) {
    32  	case int:
    33  		r = v
    34  	}
    35  	return
    36  }
    37  `
    38  
    39  // Walk parses src and walks the syntax tree. ast.Walk converts each
    40  // node to ast.Node, which looks up the itab in the itab table, and
    41  // then compares that itab with the itab of the host in a type switch.
    42  func Walk() {
    43  	f, err := parser.ParseFile(token.NewFileSet(), "src.go", src, parser.SkipObjectResolution)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	ast.Inspect(f, func(ast.Node) bool { return true })
    48  }
    49  

View as plain text