Go modules and project-local imports.

96 views
Skip to first unread message

Bruno Albuquerque

unread,
Dec 3, 2019, 12:36:49 PM12/3/19
to golang-nuts
When working with Go modules, what is the idiomatic way to deal with project-local imports (i.e. directories that are not supposed to be checked out separately from everything else. A simple example would be something like this:

project/
    library.go
    example/
        example.go

Here, project is what is supposed to be imported and there will be an associated go,mod file in the same directory.

example.go is, as the nome implies, just an example and is heavily tied to the library. It is not supposed to be imported by itself but it uses the code in library.go.

If in example.go I try doing:

import "project" (where project here is the vcs path for the library)

and then form inside the example directory I do:

go run example.go

It will fail because it will say it can not find "project"

If I add a proper go.mod file in the example directory, then it works but it actually pulls a version of project from the vcs (this is, of course, assuming there is already a committed version. otherwise it will also fail) so if I had made changes locally to library.go, they will not be seem.

So what should I do to satisfy the following requirements:

1 - go run example.go in the example directory works.
2 - Local changes to library.go will be seem when I do 1.
3 - It will still look rigth when everything is commited to vcs (meaning that if someone check it out somewhere else, 1 and 2 will still work.

Thanks in advance.

 

Andrei Avram

unread,
Dec 3, 2019, 11:54:34 PM12/3/19
to golang-nuts

Volker Dobler

unread,
Dec 4, 2019, 4:21:03 AM12/4/19
to golang-nuts
On Tuesday, 3 December 2019 18:36:49 UTC+1, Bruno Albuquerque wrote:
It will fail because it will say it can not find "project"

If I add a proper go.mod file in the example directory, then it works but it actually pulls a version of project from the vcs (this is, of course, assuming there is already a committed version. otherwise it will also fail) so if I had made changes locally to library.go, they will not be seem.

So what should I do to satisfy the following requirements:

1 - go run example.go in the example directory works.
2 - Local changes to library.go will be seem when I do 1.
3 - It will still look rigth when everything is commited to vcs (meaning that if someone check it out somewhere else, 1 and 2 will still work.

That one is dead simple. First note that example.go and library.go
are intimately coupled: The first relies on a certain version of the
later and the later requires the first to match to be a useful example.

That means these two files are part of the _same_ project:
Put the go.mod file into the project folder. (You just need one
go.mod file. not one per directory. One in the toplevel project
folder.)

Like this example.go is part of the module and it will find the project.
So 1) and 2) are satisfied.

3) is also satisfied.

V.
  
Reply all
Reply to author
Forward
0 new messages