Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang ast: Get the return type of a function call #39448

Closed
submarineCluster opened this issue Jun 7, 2020 · 2 comments
Closed

Golang ast: Get the return type of a function call #39448

submarineCluster opened this issue Jun 7, 2020 · 2 comments

Comments

@submarineCluster
Copy link

What version of Go are you using (go version)?

$ go version 1.14.1

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/shaohui/opensource/go1.14.linux-amd64/go/bin"
GOCACHE="/home/shaohui/.cache/go-build"
GOENV="/home/shaohui/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOOS="linux"
GOPATH="/home/shaohui/code/gocode"
GOROOT="/home/shaohui/opensource/go1.14.linux-amd64/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/home/shaohui/opensource/go1.14.linux-amd64/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/shaohui/code/gomod/cobra-repository/go-climate/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build599099505=/tmp/go-build -gno-record-gcc-switches"

What did you do?

About golang ast

for _, exprStmt := range m.AssignStmtList {
			for _, expr := range exprStmt.Lhs {
				switch v := expr.(type) {
				case *ast.Ident:

					//color.Blue("%v:%v \n\t[%v] %+v", meta.FullFileName,
					//	meta.FSet.Position(v.Pos()).Line,
					//	v.Name, v.Obj.Decl)
					if v.Name == "err" {
						color.Blue("%v:%v \n\t[%+v] %v, [type=%v]", meta.FullFileName,
							meta.FSet.Position(v.Pos()).Line,
							v.Obj.Type, v.Name, v.Obj.Type)
					}

					switch arg := v.Obj.Decl.(type) {
					case *ast.Field:
						color.Blue("%v:%v \n\t[%v] %v, [type=%v]", meta.FullFileName,
							meta.FSet.Position(v.Pos()).Line,
							arg.Type, arg.Names, v.Obj.Type)
					case *ast.DeclStmt:
						color.Red("XXXXXXXXXXXXXXXXXXXX")
					case *ast.AssignStmt:
						getIdent(meta, expr)
					}

				}
			}
		}

I want to get the variable type on the left side of assign. If it is error, then judge that name cannot be _

How can I get the  variable type on the left side of assign

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->



### What did you expect to see?

It sure be field of the expr of assignStmt.Lhs, but no, the expr still assignStmt, 
Too weird

### What did you see instead?

 Get the return type of a function call
@mvdan
Copy link
Member

mvdan commented Jun 8, 2020

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

To give you a head start, if you want to inspect types, you should look at the go/types package.

@mvdan mvdan closed this as completed Jun 8, 2020
@submarineCluster
Copy link
Author

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

To give you a head start, if you want to inspect types, you should look at the go/types package.

thank your, I will look at go/types package.

@golang golang locked and limited conversation to collaborators Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants