golang - creating, publishing and consuming a module
To create a module, I have the following directory/folder layout
In my root go.mod - the code looks like this
And the math.go code
Try to compile and once everything looks good, we will publish it.
To publish it
git tag v1.0.3
git push origin v1.0.3
The consuming app
You can use the following code below to consume the module above. You might need to run
go mod init myapp
go get github.com/mitzenjeremywoo/myfirstgolibrary
Also ensure that the app has the following go.mod
What if you would like to introduce v2 versioning to your module. Then perhaps your mod need to be change to include this
Notice we have /v2 at the moment
Then we would need to tag this to v2.0.0 and increment it accordingly. Otherwise go don't like it if we are still on version 1 - v1.0.4 that would not work.
In the consuming app, you can use the following code, notice the library path becomes "github.com/mitzenjeremywoo/myfirstgolibrary/v2/math" with extra v2
Complete code.
https://github.com/mitzenjeremywoo/myfirstgolibrary
Comments