7

I'm trying to understand how to organize my golang project using go1.11 modules. I tried several options, but none of them worked.

I have some code in the main package under the application folder and a local package that the main package uses.

$GOPATH
+ src
  + application/
    + main/
      + main.go
      + otherFileUnderMainPackage.go
    + aLocalPackage/
      + someCode.go
      + someCode_test.go
      + someMoreCode.go
      + someMoreCode_test.go

Files in the main package, imports ../aLocalPackage. When I compile by go build main/*.go it's working.

Then, I ran go mod init application: V.0.9.9 and got the go.mod file, but the build always fails. I always get error about not finding the local package: build application:V0.9.9/main: cannot find module for path _/.../src/application/aLocalPackage. I also tried to place the local package right under src/, place it under main/ etc. but none of these methods worked for me.

What is the way to use modules and local packages?

Thanks.

Dinesh Kumar
  • 545
  • 4
  • 17
nahsh
  • 729
  • 1
  • 6
  • 15
  • 6
    Don't use relative imports. – Volker Sep 12 '18 at 13:15
  • Did that too. got an error. I changed the import to `application/aLocalPackage` and got `cannot find module for path application/aLocalPackage` – nahsh Sep 12 '18 at 13:34
  • Where is you go.mod? Show its content. Note that "module builds" inside GOPATH are off by default. Read go help modules. – Volker Sep 12 '18 at 14:15
  • go.mod is under `application/`. its content is just `module application` + some requires. I did run `export GO111MODULE=on` – nahsh Sep 12 '18 at 14:25
  • It's working for me now, kind of... I can't use prometheus client with vgo (some packages are not loaded) so I dropped vgo from my project for now. Thanks anyway! – nahsh Sep 13 '18 at 05:50
  • Can you explain what is vgo is? – Volker Sep 13 '18 at 09:04
  • vgo was adopted into golang 1.11: https://github.com/golang/go/wiki/Modules – nahsh Sep 13 '18 at 09:47
  • Ah, Russ' demo implementation!. No need to use vgo anymore. – Volker Sep 13 '18 at 12:17

3 Answers3

2

I had some problems working with local packages myself. There are two tricks to make it work:

  1. you run "go build" in the package directory

This compiles the package and places it in the build cache. This link about code organisation in go explains more. You can identify where the cache is using:

>go env GOCACHE
/home/<user>/.cache/go-build
  1. Import using a path relative to the project

I puzzled loads over what the correct import path was and finally discovered that go doc or go list will tell you.

>go doc
package docs // import "tools/src/hello/docs"
>go list
tools/src/hello/docs

For example. I have a hello world API project and was using swaggo to generate documentation which it does in a docs sub-directory. To use it I add an import:

_ "tools/src/hello/docs"

For my case the _ is important as docs is not used directly but we its init() function to be invoked.

Now in hello/main.go I can add "tools/src/hello/docs" and it will import the correct package.

The path is relative to the location of go.mod if you have one. I have tools/ here as I have a go.mod declaring "modules tools".

Modules are a different kettle of fish - see https://github.com/golang/go/wiki/Modules. Recent versions of go (1.11 and later) can create a go.mod file which you may use to fix the version of a module that is used and avoid go's crazy default behaviour of just downloading the latest version of any package you import.

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
  • You might find this blog interesting. https://marcofranssen.nl/manage-go-tools-via-go-modules/ that is how I managed in some of my projects. – Marco Jan 21 '20 at 15:11
1

Relative import paths are not supported in module mode. You will need to update your import statements to use a full (absolute) import path.

You should also choose a module name other than application. Your module path should generally begin with a URL prefix that you control — either your own domain name, or a unique path such as github.com/$USER/$REPO.

bcmills
  • 4,391
  • 24
  • 34
0

I have written a blogpost on how to start your first Go project using modules.

https://marcofranssen.nl/start-on-your-first-golang-project/

In general it boils down to just create a new folder somewhere on your system (doesn't have to be in GOPATH).

mkdir my-project
cd my-project
go mod init github.com/you-user/my-project

This will create the go.mod file. Now you can simply create your project layout and start building whatever you like.

Maybe one of my other blogs can inspire you a bit more on how to do things.

https://marcofranssen.nl/categories/software-development/golang/

Marco
  • 4,817
  • 5
  • 34
  • 75