Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Organize local code in packages using Go modules

I can not find a way to factor out some code from main.go into a local package when using Go modules (go version >= 1.11) outside of $GOPATH.

I am not importing any external dependencies that need to be included into go.mod, I am just trying to organize locally the source code of this Go module.

The file main.go:

package main

// this import does not work
import "./stuff"

func main() {
    stuff.PrintBaz()
}

The file ./stuff/bar.go (pretending to be a local package):

package stuff

import "log"

type Bar struct {
    Baz int
}

func PrintBaz() {
    baz := Bar{42}
    log.Printf("Bar struct: %v", baz)
}

The file go.mod (command go mod init foo):

module foo

go 1.12

When executing go run main.go:

  • If I import "./stuff", then I see build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.
  • If I import "stuff", then I see build command-line-arguments: cannot load stuff: cannot find module providing package stuff.
  • If I import stuff "./stuff" with a package alias, then I see again: build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.

I can not find a way to make local packages work with go modules.

  • What's wrong with the code above?
  • How can I import local packages into other Go code inside a project defined with Go modules (file go.mod)?
question from:https://stackoverflow.com/questions/55442878/organize-local-code-in-packages-using-go-modules

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you have to choose a name for your project and write it to go.mod file. This name belongs to root directory of the project. Each new package you create must be located inside its own subdirectory and its name should match directory name.

go.mod:

module myprojectname

or (preferred way, see @typical182's answer below for details)

module github.com/myname/myproject

Then import your project's packages like:

import myprojectname/stuff

or

import github.com/myname/myproject/stuff

Files of package stuff should be located inside project's stuff directory. You name these files as you like.

Also it's possible to create deeper project structure. For instance, you decided to separate source code files from other ones (like app configs, docker files, static files, etc...). Let's move stuff directory inside pkg, every go file inside pkg/stuff still have stuff package name. To import stuff package just write:

import myprojectname/pkg/stuff

Nothing stops you from creating more levels in the hierarchy like github.com/myuser/myproject/pkg/db/provider/postgresql, where:

  • github.com/myuser/myproject - project name.
  • postgresql - package name.
  • pkg/db/provider/postgresql - path to the package relative to project's root.

You can read more about go modules here: https://github.com/golang/go/wiki/Modules

Check out this repository to get useful information about various patterns used in project organizing: https://github.com/golang-standards/project-layout If you go inside pkg directory you will find out which open source projects use pkg directory in their structure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...