Skip to content

Instantly share code, notes, and snippets.

@roscopecoltran
Forked from tenntenn/findgopher.go
Created October 26, 2018 14:19
Show Gist options
  • Save roscopecoltran/52f435016c3851dc4dd9e6fac83a5a02 to your computer and use it in GitHub Desktop.
Save roscopecoltran/52f435016c3851dc4dd9e6fac83a5a02 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
)
func main() {
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "gopher.go", nil, 0)
if err != nil {
log.Fatal(err)
}
ast.Inspect(f, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.Ident:
if n.Name == "gopher" {
fmt.Println(fs.Position(n.Pos()))
}
// TODO: caseを追加する
}
return true
})
}
package main
import "fmt"
type Gopher struct {
gopher string `json:"gopher"`
}
func main() {
const gopher = "GOPHER"
gogopher := GOPHER()
gogopher.gopher = gopher
fmt.Println(gogopher)
}
func GOPHER() (gopher *Gopher) {
gopher = &Gopher{
gopher: "gopher",
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment