Hacker News new | ask | show | jobs
by azhenley 2896 days ago
I recently started to learn Go and everything has gone smoothly... except the bizarre, unfriendly GOPATH and forced project structure.

It was not fun when VS Code (with the Go plugin) would automatically remove my imports every time I saved the file because it couldn’t find it.

2 comments

Removing imports sounds like a gofmt issue. That is the linter / formatter for all Go code. If you don't run the linter and try to build the code, it won't work anyway. For example, I added the net/http package to a file without using it and got this error when running `go build` without doing `go fmt` first:

    ./example.go:8:9: imported and not used: "net/http"
So VS Code's Go plugin removes the unused import because otherwise the code is invalid. I'm a vim user and `vim-go` has the same behavior.
I like the consistent, sane project structure, but GOPATH is an odd duck to be sure.
I actually started using the GOPATH structure for all my repos after discovering three full checkouts of github.com/torvalds/linux [1] on my drive.

[1] I know it's not the official upstream remote, but I find this one the easiest to remember.

I do the same, but I don't like that it is searched by default when building a Go program. It's too easy for the versions of dependencies in those directories to have changed. And even if you vendor, it falls back to GOPATH so you can still bring things into your build by accident.