my package not include in go modules

120 views
Skip to first unread message

Ali Hassan

unread,
Jul 25, 2020, 5:03:44 AM7/25/20
to golang-nuts
I want to import libraries in module file but modules add all other libraries except those packages which I had created, DB is one of them. How to import? 
Error, please help me to resolve 

Christoph Berger

unread,
Jul 28, 2020, 8:45:23 PM7/28/20
to golang-nuts
A module can consist of multiple packages. The packages in your repository all belong to the module in the root directory of the repo (where your go.,mod file is). And there is nothing wrong with this. You can maintain them all together in the same module.

However, perhaps you want to make one or more packages available for other projects. Then it makes sense to move them into their own module.

To use separate modules for your packages, you can do either of these two approaches:

Option 1 (preferred). Move your non-main packages into separate repositories. Run go mod init <package path> at the root of each repo to create a module for each package. (Here we have one repo = one package = one module.)

Option 2. Keep all packages in your single repo. For each non-main package, cd into the package dir and run go mod init <package path>. Caveat: This way you get nested modules, which is probably not something you really want. See the multi-module repositories FAQ in the Go Wiki.for some reasons against multi-module repos. Plus a cite from Russ Cox: "For all but power users, you probably want to adopt the usual convention that one repo = one module."

– Christoph

Brian Candler

unread,
Jul 29, 2020, 8:12:47 AM7/29/20
to golang-nuts
A Go module is a collection of packages.  The packages are in a tree of directories (Go requires all files in one directory to be part of the same package), and there is a go.mod file at the root of the tree.

Users import packages individually, not the whole module.  go.mod documents the dependencies of this module, not the packages which this module contains.

Perhaps a real-world example will be helpful.  I often use the "assert" package from the "github.com/stretchr/testify" module:

package main

import (
"testing"
)

func TestSanity(t *testing.T) {
    assert.Equal(t, 2, 1+1)
}

Have a look carefully at the import statement.  The package I am importing is this directory of files:

Each of those files contains "package assert" - it's conventional (but not required) that the package name match the directory name. However, there is no go.mod in that directory.

You will find go.mod in the parent directory:

This directory could contain go source files, although in this case it does not.  If it did contain the source for another package, the import path for that package would be https://github.com/stretchr/testify.

Marvin Renich

unread,
Jul 29, 2020, 1:23:07 PM7/29/20
to golang-nuts
* Ali Hassan <alideve...@gmail.com> [200725 01:04]:
> I want to import libraries in module file but modules add all other
> libraries except those packages which I had created, DB is one of them. How
> to import?
> Error <https://github.com/ali2210/WizDwarf/tree/master/db>, please help me
> to resolve

Also note that any go command that needs to download the package must
download the whole repo. Downloading happens for go get and for a
number of go commands, such as go build and go test, when the package
has not already been downloaded and cached. Using one repo = one module
= one package means that downloads and disk space for cached repos can
be smaller.

However, if several packages are closely related and will typically be
used together, putting them in the same module (and repo) makes sense.

...Marvin

Reply all
Reply to author
Forward
0 new messages