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/parser : parser go src file then printf to file, format error #64927

Closed
LY1806620741 opened this issue Jan 2, 2024 · 2 comments
Closed

go/parser : parser go src file then printf to file, format error #64927

LY1806620741 opened this issue Jan 2, 2024 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@LY1806620741
Copy link

Go version

go version go1.21.5 linux/amd64

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

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/dev/.cache/go-build'
GOENV='/home/dev/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/dev/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dev/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/usr/local/go'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/dev/Desktop/mkcert-autoconfig/i18ntools/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3963334977=/tmp/go-build -gno-record-gcc-switches'

What did you do?

main.go

        sourcepath := "../cert.go"
	node, _ := parser.ParseFile(fset, sourcepath, nil, parser.ParseComments|parser.AllErrors)
        file, _ := os.OpenFile("cert_edit.go", os.O_CREATE|os.O_WRONLY, os.ModePerm)
	defer file.Close()
	if err := printer.Fprint(file, fset, node); err != nil {
		log.Fatalln("Error:", err)
	}

cert.go from https://github.com/LY1806620741/mkcert-autoconfig/blob/111715b04f1ff91ff297b9652fcc31e5ab3ff4f1/cert.go#L37

What did you expect to see?

new file cert_edit.go the same as cert.go

What did you see instead?

go parser rewrite cert.go code but duplicate code inserted. But I didn't operate the ast tree.
image

@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 2, 2024
@mauri870
Copy link
Member

mauri870 commented Jan 2, 2024

I just tried this locally and it appears to be working as expected.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@mauri870 mauri870 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2024
@LY1806620741
Copy link
Author

I just tried this locally and it appears to be working as expected.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

Thank you for your reply. This issue is caused by not resetting the content of the opened file.

file:=os.OpenFile
//resolvent
file.Truncate(0)
file.write

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants