'go build' over writing source code.

110 views
Skip to first unread message

varshn...@gmail.com

unread,
Sep 5, 2019, 12:48:01 PM9/5/19
to golang-nuts
Hi,

I downloaded go version 1.13 and tried to create new test project outside GOPATH. Following is my observation.
Should I open a bug or it is expected/known behavior?

What version of Go are you using (`go version`)?
$ go version
go version go1.13 linux/amd64

Does this issue reproduce with the latest release?
yes


What operating system and processor architecture are you using (`go env`)?
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/abhishek/myprog/golang/bin"
GOCACHE="/home/abhishek/.cache/go-build"
GOENV="/home/abhishek/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/abhishek/myprog/golang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/abhishek/software/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/abhishek/software/go/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-build290715438=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

What did you do?
1. Create folder testmux in non GOPATH folder.
2. Create main.go file inside testmux.
3. Execute go mod init
4. Execute go build

code for 'main.go' is present at:
https://play.golang.org/p/nzbMB2v48ty

Other Observation
Executing same steps inside $GOPATH/src, create binary testmux. It does not over write main.go.

What did you expect to see?
On command 'go build' inside project folder binary with project folder name should be present.

What did you see instead?
Command 'go build' over writes main.go command with binary (ELF) format. The source code is gone now. Not recoverable.

Regards,
Abhishek Varshney

Ian Lance Taylor

unread,
Sep 5, 2019, 12:57:44 PM9/5/19
to varshn...@gmail.com, golang-nuts
Where exactly did you create the folder testmux? What were the exact
commands that you ran? What was the exact output?

I tried to follow your direction.

> mkdir /tmp/x
> cd /tmp/x
> <create main.go with your contents>
> go mod init
go: cannot determine module path for source directory /tmp/x (outside
GOPATH, module path must be specified)

Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.

What did you see when you ran "go mod init"?

Ian

Abhishek Varshney

unread,
Sep 5, 2019, 10:58:34 PM9/5/19
to golang-nuts

Thanks for replying. I understood the my mistake. I executed wrong 'go mod' command. By mistake I used 'go mod init main.go'.
Thank you.
Reply all
Reply to author
Forward
0 new messages