Need help with go modules

514 views
Skip to first unread message

web user

unread,
May 4, 2020, 9:59:22 AM5/4/20
to golang-nuts
I have a personal project using GOPATH

But recently, I wanted to use go-redis for a project and v7 forces you to use go mod. So I figured, I'd migrate the account over to go mod. But I'm stuck. 


My directory structure is:

~HOME/src/myrepo
~HOME/src/myrepo/cmd/cmd1
~HOME/src/myrepo/commong
~HOME/src/github

So I did the following command: cd ~HOME/src/myrepo; go mod init ~HOME/src/myrepo

when I run the command go build ~HOME/src/myrepo/cmd/cmd1/...

It does not update the go.mod in the directory ~HOME/src/myrepo? Do I need a go.mod in ~HOME/src/myrepo/cmd/cmd1


Amnon Baron Cohen

unread,
May 5, 2020, 7:28:55 AM5/5/20
to golang-nuts
Interesting. At first sight this should work.

You definitely don't need a go.mod file in ~HOME/src/myrepo/cmd/cmd1

Which go version are you running?

What is your $GOPATH set to?

What output does cd ~HOME/src/myrepo; go build give?

The usual convention is to push the code in a VCS such as github and use the vcs path as an argument to go mod init

e.g.
go mod init github.com/myuser/myrepo

- Amnon

lj01...@gmail.com

unread,
May 22, 2020, 7:59:03 AM5/22/20
to golang-nuts
It looks like I am not only one to struggle with (new?) go modules. 
I still consider me as novice to GO but the major problem is as usual, the focus. 

1. Modules are complicated to understand
2. Lack of examples (of real use, not just POC)
3. Focus on extra (scale up) details (that are surely important) and none of focus on basic things (which are essentially more important as they make things working and not working)

I have spent several days in attempt to make a working module and I failed.
I learned a lot of things on the way but still did not manage to make a working example.
The working example in my understanding shall be given as:

Variant 1: Making executable

1. how the main (executable) uses the module (import and function call, how to avoid function name conflicts?)
2. how to build a module that 1.(main program uses)
3. how and where to compile and what and what compilation output to expect

Variant 2: Making library

In a way a module is a library but, one may want to make his own library that will be used in his project and within that library use his custom modules, so we go the same

1. how library modules uses the custom module (import and function call and how to avoid function name conflicts from different modules?
and 
2. which shall be the same as for Variant 1. How really to make a working module.
3. which shall be the same as for Variant 1. how and where to compile and what and what compilation output to expect

this is more a feedback on what conceptually I am struggling with go and modules

-------

On the other note the relation between gopath and modules. Currently (I am trying to use go 1.14) I understand that there is a method of 'gopath' where go will seek for libraries. This goes to compilation part.

Say have i have a custom module site/name/module (literally)

what i put in package part? what would be relation between main and others. In others - is there package site/name ?
where to put sources so go finds them?  (while compilation gopath method suggests where to put sources - it is a help but is this correct way of doing things?
how to compile these sources so it is explained which modules are used (and from where)?
if I just compile a module - what do I get ? If I get some sort of an object - where to look for it? How it is called? How do I reference it from the source.

For example in my experimental trials I get (a hello world executable trying to call function defined in a module)

I am getting error: 'site/name/module' imported and not used
of course since module is not found
I also get error: undefined: Hello 
which is a function I try to call.

Hope all of these make sense, and possibly someone wrote somewhere something of these but so far I did not find it. And I LIKE reading documentation, but documentation is just partially helpful.

Thanks





Amnon Baron Cohen

unread,
May 23, 2020, 3:11:01 AM5/23/20
to golang-nuts
I would work through

https://golang.org/doc/code.html

- Amnon

Henry

unread,
May 24, 2020, 12:58:02 AM5/24/20
to golang-nuts
Here is a brief tutorial for those new to Go Module. This is by no means a comprehensive guide to Go Module, but I hope this should be enough to get you started.

What is Go Module, and why you should be using one? Go Module is a dependency management tool. The primary benefit of Go Module is that it allows you to specify specific versions of your dependencies. That way you can ensure your project does not break when some of your dependencies change their APIs. Compare this with GOPATH that always assume you want to use the latest versions of your dependencies. The other benefit of Go Module is that you can set up your projects outside of your GOPATH folder structure. If you are migrating existing GOPATH projects into Go Module, you will save a lot of headache by moving them out of your GOPATH folder.

Next, we need to understand versioning. A version is normally written as v1.2.3 where 1 is the major number, 2 is the minor number, and 3 is the patch number. The general convention adopted by Go community is that you should increase the major number if there is a breaking change. Increase the minor number, if you are adding new features. Increase the patch number for bug fixing. Understanding this versioning convention is important so that you know what to expect when you specify your dependency's version, and to keep your library compatible with everyone else's expectation.

Go Module is just a configuration file inside your project called go.mod that specifies other go tools what to do with your projects. Go build tools usually create another file (go.sum) but you normally do not need to mess with this. If you accidentally deleted go.sum, your project will still build just fine as long as your go.mod is there. You can manually edit your go.mod, but for the purpose of this guide, I will stick to using tools whenever possible, because it is easier. 
 
To create a Go Module, step inside your project folder, and type go mod init <namespace> where <namespace> is the qualified name of your project. For instance, if you want to call your project "github.com/henry/myproject", then inside your myproject folder, type go mod init github.com/henry/myproject

To add a dependency, just code away as you normally would. Every time you build, go build tools will examine your import lines and automatically add those dependencies into your go.mod. If your dependency is stored somewhere else that Go tools can't find (eg. in your local computer), you need to manually specify where to find it. Use go mod edit -replace <name>=<path>, where <name> is the qualified name of your dependency and <path> is where it is located. 

To upgrade your dependencies, you can issue go get -u all command, or use go get -u <pkg> to upgrade a specific package. There are also third party tools to help check for outdated dependencies. 

To version your project, simply add a tag in your git commit and upload your project. Go Module will recognize this. For instance, if you want to commit your project to version 0.2.0, simply add tag "v0.2.0" to your commit. This applies to major version number 0 and 1. For major version number 2 (eg. v2.0.0) upwards, see below.

For version major 2 upwards, instead of simply tagging your commit as usual, you need a special project structuring. For version 2.0.0, add folder v2 into your project. If your are upgrading into major version 3, add folder v3, and so on. Assuming we use the above github.com/henry/myproject, now add v2 folder so that it becomes myproject/v2. Inside the v2 folder, start a new go mod. In our example, you would type go mod init github.com/henry/myproject/v2 . Note the v2 in the end. Add your version 2 stuffs into the v2 folder and code away as usual, commit, tag and upload.

Now if you want to use your version 2 library in your other projects, simply change your import lines to include the v2. So, in our example, change your import line from "github.com/henry/myproject" to "github.com/henry/myproject/v2". Go build tools will recognize this and automatically use the version 2 of your library.

Note that you can always manually edit your go.mod.

For further reading, see Using Go Modules

I hope this helps.

Henry

Jan Mercl

unread,
May 25, 2020, 1:04:16 AM5/25/20
to Henry, golang-nuts
On Sun, May 24, 2020 at 6:58 AM Henry <henry.ad...@gmail.com> wrote:
> To create a Go Module, step inside your project folder, and type go mod init <namespace> where <namespace> is the qualified name of your project. For instance, if you want to call your project "github.com/henry/myproject", then inside your myproject folder, type go mod init github.com/henry/myproject

s/<namespace>/import path/. Namespaces as well as "qualified name of
your project" have no definition in the specs. Import path has:
https://golang.org/ref/spec#Import_declarations

Henry

unread,
May 26, 2020, 8:53:56 AM5/26/20
to golang-nuts
You are right. Thanks for the correction. Import path is the correct Go term.

On Monday, May 25, 2020 at 12:04:16 PM UTC+7, Jan Mercl wrote:
Reply all
Reply to author
Forward
0 new messages