Hacker News new | ask | show | jobs
by always_good 2859 days ago
All three replies to me suggest that you can do relative imports in Go.

Can you link me to documentation that demonstrates them?

I don't understand. Either relative imports are not supported or they only work in some context that nobody uses. BTW I'm not a Go beginner, and my issues with Go aren't just an issue with "getting started" as you suggested.

I just tried them in an existing Go project and I get the usual "can't load package" error.

Two random links:

- https://stackoverflow.com/questions/38517593/relative-import... "No there is no relative import in Golang."

- https://github.com/golang/go/issues/20883 "Support relative imports in Go 2.0"

> So you can go into your GOPATH (or vendor dir) and checkout your fork in place of the origin

Imagine the real-world case where you're working on more than one project at a time. Project X and Project Y both depend on a Util project on GOPATH, but they require different versions of Util.

Each time you switch projects, you cd into GOPATH and check out the right version of Util?

I'm sure this stuff works for a megarepo like the one Google has. But for the rest of us, this stuff was solved by package managers. A lesson Go ended up having to learn in the end.

2 comments

Relative imports work, but keep in mind the import works based on package name. So "import ./foo", you must have a package called "foo" in "./foo"

----

Yes, it is easy to forget that we use vendoring to get around these issues (and for other reasons).

By the way, wasn't trying to suggest that you are new to go, just that I understand there is some tribal knowledge involved that make it difficult for new comers and people who don't spend much time working with the language.