Go Workspace

Go workspace is a new feature released in Go 1.18 that enables developers to manage local dependencies in a single workspace. It's an excellent step for Go toward improving mono-repo support.

Elon Salfati profile picture
Elon Salfati
@elonsalfati

Starting from Go 1.18, Go added multi-module support to the language. Multi-module or workspaces allow us, developers, to boost our mono-repo game up. If you are familiar with NodeJS, the Go workspace brings the same functionality as npm/yarn workspaces. Leveraging this methodology can ensure higher module decoupling, simplify maintenance, and improve our mono-repo game (Huge fan of mono-repo here). However, if you come from NodeJS, don't expect the same maturity of go workspace just yet.

Pre Requirements

  1. I assume that you're already familiar with the basics of Go.
  2. Go 1.18 or later
  3. Any tool to edit your code
  4. A terminal

Create the first module

Create a Go workspace directory in your development folder.

$ mkdir gowork-example$ cd gowork-example

We're going the follow a structure similar to what turborepo for NodeJS:

+ apps/
|   + app/
|
+ pkgs/
|   + logger/
+ go.work    We'll get to this file later

Let's create the main module under apps/app and then initialize our first go module

$ mkdir -p apps/app$ cd apps/app$ go mod init github.com/$YOUR_ACCOUNT/gowork-example/app$ touch main.go

In our main go, we'll simply create an elementary hello-world example for now.

main.go

package mainimport "fmt" // We'll replace this package with our logger custom package soonfunc main() {    fmt.Println("Get Schwifty... πŸ’β€β™‚οΈ")}

Now when we run our package, we'll be able to see our output

# From the apps/app folder$ go run main.goGet Schwifty... πŸ’β€β™‚οΈ

Adding the logger module

In the pkgs/logger folder, we'll initialize our go module and create the main logger file with a simple function.

# from the project's root directory$ cd ./pkgs/logger$ go mod init github.com/$YOUR_ACCOUNT/gowork-example/logger$ touch logger.go

In the logger file, we'll add one simple function that prints the given message and adds the [INFO] prefix.

package loggerimport "fmt"func log(msg string) {    fmt.Printf("[INFO] %s\n", msg)}

The moment of truth - Connecting the modules

Let's talk about go.work - this is a new file in the go ecosystem that creates a reference to all available modules. To create this file, we'll use the go work init command from the project root.

$ go work init

This command will create the go.work file in the project's root. In this file, we'll find a reference to the Go version. However, our work is not yet completed. To add a package to the workspace, we'll need to add each package using the go work use $PATH command.

Let's add our app and logger to the workspace by running the following commands from the project's root folder.

$ go work use ./apps/app$ go work use ./pkgs/logger

After running these commands, we'll see that our go.work file has changed, and now it contains a reference to the packages we've just added and should look like this:.

go 1.18

use (
    ./apps/app
    ./pkgs/logger
)

Updating our original module to use our logger

Let's update the main.go file, and instead of using fmt.Println directly, we'll leverage our logger.

package mainimport "github.com/$YOUR_ACCOUNT/gowork-example/logger" //  The package from our workspacefunc main() {    logger.log("Get Schwifty... πŸ’β€β™‚οΈ")}

When running this application, the output should now have our identifier prefix.

# From the apps/app folder$ go run main.go[INFO] Get Schwifty... πŸ’β€β™‚οΈ

Wrapping it up

Go workspace provides a big step towards managing multiple repositories in a much more reusable way. However, if you come from languages like NodeJS, you know there's still a long way to go. Tools like lerna or turborepo bring mono-repo and workspace to a whole other level (if you want to invest a bit into open source πŸ˜‰). Yet still, Go workspace is an excellent step in the right direction.


Salfati Group

Salfati Group is a holding company that provides products and services in the software architecture, cloud, and cybersecurity space. We've adopted go workspace completely, allowing us to decouple and reuse our go modules elegantly.

Nopeus

By enabling a cloud application layerβ„’, Nopeus can help manage your cloud infrastructure with 10x less resource and time. In addition, Nopeus, our latest project, pushed us to adopt Go and Go workspace even further.

Check our GitHub project at https://github.com/salfatigroup/nopeus. You can also check nopeus cloud for more advanced features.

Follow me on Twitter (@elonsalfati) to follow my journey #buildinginpublic Salfati Group and Nopeus


Get all of our updates directly to your inbox.
Sign up for our newsletter.

Salfati Group, Limited.

Our mission is to empower your
product security and velocity.

Products