Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 14, 2022 10:45 am GMT

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

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

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

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

$ go run main.goecapskroW olleH

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

This will create a go.work file.

$ cat go.workgo 1.18

We will also add our hello module to the workspace.

$ go work use ./hello

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

go 1.18use ./hello

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

$ git clone https://go.googlesource.com/exampleCloning 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.

example/stringutil/reverse.go

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

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

$ go work use ./example$ cat go.workgo 1.18use (    ./example    ./hello)

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

$ go run helloI can do whatever!! Hello Workspace

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 / go-course

Master the fundamentals and advanced features of the Go programming language

Go Course

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 as well as on Educative.io

banner

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, and safety of a statically typed and compiled language with the ease


Original Link: https://dev.to/karanpratapsingh/go-course-workspaces-2lcb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To