DEV Community

Cover image for Go Course: Workspaces
Karan Pratap Singh
Karan Pratap Singh

Posted on • Originally published at karanpratapsingh.com

Go Course: Workspaces

In this tutorial, we will learn about multi-module workspaces that were introduced in Go 1.18.

Workspaces allow us to work with multiple modules simultaneously without having to edit go.mod files for each module. Each module within a workspace is treated as a root module when resolving dependencies.

To understand this better, let's start by creating a hello module.

$ mkdir workspaces && cd workspaces
$ mkdir hello && cd hello
$ go mod init hello
Enter fullscreen mode Exit fullscreen mode

For demonstration purposes, I will add a simple main.go and install an example package.

package main

import (
    "fmt"

    "golang.org/x/example/stringutil"
)

func main() {
    result := stringutil.Reverse("Hello Workspace")
    fmt.Println(result)
}
Enter fullscreen mode Exit fullscreen mode
$ go get golang.org/x/example
go: downloading golang.org/x/example v0.0.0-20220412213650-2e68773dfca0
go: added golang.org/x/example v0.0.0-20220412213650-2e68773dfca0
Enter fullscreen mode Exit fullscreen mode

And if we run this, we should see our output in reverse.

$ go run main.go
ecapskroW olleH
Enter fullscreen mode Exit fullscreen mode

This is great, but what if we want to modify the stringutil module that our code depends on?

Until now, we had to do it using the replace directive in the go.mod file, but now let's see how we can use workspaces here.

So let's create our workspace in the workspace directory.

$ go work init
Enter fullscreen mode Exit fullscreen mode

This will create a go.work file.

$ cat go.work
go 1.18
Enter fullscreen mode Exit fullscreen mode

We will also add our hello module to the workspace.

$ go work use ./hello
Enter fullscreen mode Exit fullscreen mode

This should update the [go.work](http://go.work) file with a reference to our hello module.

go 1.18

use ./hello
Enter fullscreen mode Exit fullscreen mode

Now, let's download and modify the stringutil package and update the Reverse function implementation.

$ git clone https://go.googlesource.com/example
Cloning into 'example'...
remote: Total 204 (delta 39), reused 204 (delta 39)
Receiving objects: 100% (204/204), 467.53 KiB | 363.00 KiB/s, done.
Resolving deltas: 100% (39/39), done.
Enter fullscreen mode Exit fullscreen mode

example/stringutil/reverse.go

func Reverse(s string) string {
    return fmt.Sprintf("I can do whatever!! %s", s)
}
Enter fullscreen mode Exit fullscreen mode

Finally, let's add example package to our workspace.

$ go work use ./example
$ cat go.work
go 1.18

use (
    ./example
    ./hello
)
Enter fullscreen mode Exit fullscreen mode

Perfect, now if we run our hello module we will notice that the Reverse function has been modified.

$ go run hello
I can do whatever!! Hello Workspace
Enter fullscreen mode Exit fullscreen mode

This is a very underrated feature from Go 1.18 but it is quite useful in certain circumstances.


This article is part of my open source Go Course available on Github.

GitHub logo karanpratapsingh / learn-go

Master the fundamentals and advanced features of the Go programming language

Learn Go

Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.

This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!

Table of contents

What is Go?

Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.

It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed…

Top comments (0)