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 
<https://blog.golang.org/using-go-modules>

I hope this helps.

Henry

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3fe73178-52f7-4da9-b1bd-5170e1882ffd%40googlegroups.com.

Reply via email to