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

go/ast: Empty field docs and comments for function arguments #35484

Closed
index0h opened this issue Nov 9, 2019 · 1 comment
Closed

go/ast: Empty field docs and comments for function arguments #35484

index0h opened this issue Nov 9, 2019 · 1 comment

Comments

@index0h
Copy link

index0h commented Nov 9, 2019

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

go version go1.13.4 linux/amd64

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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/index0h/.cache/go-build"
GOENV="/home/index0h/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/index0h/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/4762"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/4762/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build225761439=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Hi,
I tried to get comment for argument from it's func declaration, but ast.Field.Comment and ast.Field.Doc have nil values. Also, I tried the same for struct fields and comments there are exist.

package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
)

const content = `
package my_package

func MyFunc(
	// FuncComment
	argument string,
) {}
`

func main() {
	fileSet := token.NewFileSet()
	astFile, _ := parser.ParseFile(fileSet, "main.go", content, parser.ParseComments)

	fmt.Printf("Found comment in astFile: %s\n", astFile.Comments[0].Text())

	funcArgument := astFile.Decls[0].(*ast.FuncDecl).Type.Params.List[0]
	fmt.Printf("Found doc in func argument: %s\n", funcArgument.Doc.Text())
	fmt.Printf("Found comment in func argument: %s\n", funcArgument.Comment.Text())
}

Output

Found comment in astFile: FuncComment

Found doc in func argument: 
Found comment in func argument: 

What did you expect to see?

ast.Field.Doc for func argument should contain comment if it exists in source.

What did you see instead?

Empty ast.Field.Doc and ast.Field.Comment for existed comments in source.

@agnivade
Copy link
Contributor

You have to use the Comments field to get the comments.

package main

import (
	"fmt"
	"go/parser"
	"go/token"
	"log"
)

const src = `
package my_package

func MyFunc(
	// FuncComment
	argument string,
) {}

`

func main() {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err != nil {
		log.Fatal(err)
	}

	for _, c := range f.Comments {
		fmt.Printf("%v\n", c.Text())
	}

}

Look into https://golang.org/pkg/go/ast/#CommentMap for more information.

@golang golang locked and limited conversation to collaborators Nov 9, 2020
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