
Transferring your projects to module go
With go 1.16, the modules system is now the go to system in go. Old projects that worked well in 1.15* will not build now. In order to build your projects under 1.16, you’ll need to commit to the new module system.
Just a little about go modules
As this is not a post about go modules, I won’t be amplifying on this topic too much; However, this topic does has a lot to do with this post, so here is the gist of it:
- It relieves you from the need to set GOPATH for your current project; That was a major pain, although a little make trick got rid of it.
- Way more important – it now lets you manage dependencies; The go kinda version of NPM if you will. And yes, a make trick solved that too, but nothing beats the native tool-chain.
- It is simple to create a new project, or to convert an existing old one!
What changed?
- File and folder structure, no more src folder!
- The project root is now actually the project root.
- The module name is a part of the naming for your packages, beside main.
- go install – will now push the payload to $GOPATH/bin and not a local bin folder, so if you want it locally, you’ll need to use the -o option – which I use (of course) in a make script.
Let’s get coding!
A very simple go project that was compiled under go 1.15 (with all the make goodies) can be found in the git link here. It’s a classic old go sample (hence the name). Makefile to build (And even edit), src folder, the project folder (sample), with the main file, containing the main function. In a sub folder, there is the mathpack (to not confuse with go’s math). mathpack is not going to win a Turing award, but it does work (under go 1.15 and below). You can build this “project” using make, or run it using make run. If you’ll install the current version of go and try to build it, you’ll encounter this message:
go install: version is required when current directory is not in a module
Try 'go install sample@latest' to install the latest version
make: *** [Makefile:2: default] Error 1
So, let’s transform this code to the go modules system!
First thing: create a go module using mod:
Code language: Bash (bash)go mod init talreg.com/sample<br>go mod tidy
Now, we have our go module! But it won’t work, trying to build it will yield:
package sample is not in GOROOT (/usr/local/go/src/sample)
Code language: Bash (bash)
So, let’s first move all the files and folders from src/sample to the root of the project (where the Makefile is):
Code language: Bash (bash)mv src/sample/* .
We can remove the src and it’s subfolder(s):
Code language: Bash (bash)rm -rf src/
Now, we’re almost done! the only thing needs to be done is updating the import path. Open main.go in your editor and change
"sample/mathpack"
Code language: Go (go)
In line 5 to:
"talreg.com/sample/mathpack"
Code language: Go (go)
Now the Makefile wouldn’t work (it’s still go 1.15) but you can build it yourself:
Code language: Bash (bash)go build -o bin/sample
Let’s update the Makefile
Replace the Makefile line 2 with the build command we use. The edit commands are no longer needed! To test we no longer need the GOPATH trick. just plain
Code language: Makefile (makefile)go test ./...
Will work.
Now, you can make, run and test your project! You can look at the code and the new structure under the main branch.