Why does not golang like local imports

I have faced several times when go mark my import and says it is local import

If you mean relative imports like ../foo these are not recommended. Instead use a full import path like github.com/example/foo.

(If you mean something else, please clarify what your code is and what error you get.)

I mean relative imports, thanks! Why they are not recomended?

For one reason, because it doesn’t say what the import is. If you see a import "../foo" that doesn’t mean anything to you unless you have the same stuff on disk as I have. Giving you just a file is impossible as it removes the context. Tooling doesn’t understand it. It doesn’t work well with modules. And so on.

2 Likes

It is possible to work entirely outside of VCS on my local filesystem.

See,https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem?

For me, i prefer to use shorter package name, instead something like “some_vcs.url/my_name/my_package”.

Here is my directories:

./
./common
./common/go.mod   (module local/common)

./service
./service/go.mod
>module local/service
>require local/common v0.0.0  
>replace local/common => ../common        <HERE

./service/main.go    
>package main
>import "local/common"

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.