loading...

Intro to Modules on Go - Part 2

prassee profile image prassee ・2 min read

In the previous post we saw how to init modules, write one, publish it (locally with replace directive) & use it on another Go project / module. This part we are going to publish the module to github and get rid of replace way of aliasing.

Publishing to Github

Publshing to github is a no-brainer just init a git repo and push it to github. However the name of the repo does matters here since the consumers of our module would use that as the identifier.

Lets change the go.mod file on localmod1. So that it points to a proper git repo than just a name.

module github.com/prassee/localmod1

go 1.12

now lets push it to github.

git init . 
git remote add origin git@github.com:prassee/localmod1.git
...
git cm 'initial commit' 
git push origin master

Now the module is published to github. Lets proceed further to use it on other modules.

Using in other modules

This step does not require any coding but we just need to edit go.mod file. Lets open usingmod1 module's
go.mod file.

module usingmod1

replace github.com/prassee/localmod1 => ../localmod1

go 1.12

require github.com/prassee/localmod1 v0.0.0-00010101000000-00000000
0000 // indirect

in the above we just need to replace the 2nd line with the following and updated version should look like below.

module usingmod1
require github.com/prassee/localmod1 v1.0.2
go 1.12

now if we run go mod tidy. we can see its downloading the artifact with the version as we desired.

> go mod tidy
go: finding github.com/prassee/localmod1 v1.0.2
go: downloading github.com/prassee/localmod1 v1.0.2
go: extracting github.com/prassee/localmod1 v1.0.2

Notes

  • Across this excercise we never used go get command or had any vendor folder to manage our dependenceis.
  • Modules were written outside the GOPATH.

Thats all folks .... I think I had covered the extent I know about Go Modules.

Posted on by:

Discussion

markdown guide