Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What Is the Go Workspace Mode

Yuki Ito
February 18, 2022

What Is the Go Workspace Mode

Yuki Ito

February 18, 2022
Tweet

More Decks by Yuki Ito

Other Decks in Programming

Transcript

  1. Background gwdemo1 gwdemo2 package main import ( "fmt" "github.com/110y/gwdemo3" )

    func main() { fmt.Println(gwdemo3.Hello("Workspace")) } package main import ( "fmt" "github.com/110y/gwdemo3" ) func main() { fmt.Println(gwdemo3.Hello("Generics")) }
  2. Background When we add a new feature to gwdemo3... package

    gwdemo3 import "fmt" func Hello(s string) string { return fmt.Sprintf("Hello, %s!", s) } func Bye(s string) string { return fmt.Sprintf("Bye, %s!", s) }
  3. Background And want to use it on gwdemo1 and gwdemo2

    locally... package main import ( "fmt" "github.com/110y/gwdemo3" ) func main() { fmt.Println(gwdemo3.Hello("Workspace")) fmt.Println(gwdemo3.Bye("Workspace")) }
  4. Background We have to add a replace directive to the

    go.mod fi le module github.com/110y/gwdemo1 go 1.18 require github.com/110y/gwdemo3 v0.0.0-20220218011518-502c64a9beed replace github.com/110y/gwdemo3 => ../gwdemo3
  5. Background Problem ɾHave to add replace directive to each module's

    go.mod fi le. ɾHave to remove replace directive before submitting the code.
  6. Background But working with the replace directive can often be

    awkward: each module developer might have working versions at di ff erent location on disk, so having the directive in a fi le that needs to be distributed with the module isn't a good fi t for all use cases. https://go.googlesource.com/proposal/+/master/design/45713-workspace.md Proposal: Multi-Module Workspaces in cmd/go
  7. Workspace Mode $ mkdir gwdemo-workspace $ cd gwdemo-workspace $ git

    clone https://github.com/110y/gwdemo1.git $ git clone https://github.com/110y/gwdemo2.git $ git clone https://github.com/110y/gwdemo3.git Workspace
  8. Workspace Mode go work init creates go.work fi le go

    1.18 use ( ./gwdemo1 ./gwdemo2 ./gwdemo3 ) go.work
  9. Workspace Mode $ tree . . ├── go.work ├── gwdemo1

    │ ├── go.mod │ ├── go.sum │ └── main.go ├── gwdemo2 │ ├── go.mod │ ├── go.sum │ └── main.go └── gwdemo3 ├── go.mod └── gwdemo3.go
  10. Workspace Mode $ go help work ... To determine whether

    the go command is operating in workspace mode, use the "go env GOWORK" command. This will specify the workspace file being used. ... Workspace
  11. Workspace Mode package gwdemo3 import "fmt" func Hello(s string) string

    { return fmt.Sprintf("Hello, %s!", s) } func Bye(s string) string { return fmt.Sprintf("Bye, %s!", s) } gwdemo3
  12. Workspace Mode package main import ( "fmt" "github.com/110y/gwdemo3" ) func

    main() { fmt.Println(gwdemo3.Hello("Workspace")) fmt.Println(gwdemo3.Bye("Workspace")) } gwdemo1
  13. Workspace Mode $ cd gwdemo1 $ go env GOWORK /path/to/gwdemo-workspace/go.work

    $ go run . Hello, Workspace! Bye, Workspace! Workspace
  14. Problem ✅ Have to add replace directive to each module's

    go.mod fi le. ✅ Have to remove replace directive before submitting the code. Workspace Mode
  15. Workspace Mode $ cd gwdemo1 $ go env GOWORK /path/to/gwdemo-workspace/go.work

    $ GOWORK=off go run . # github.com/110y/gwdemo1 ./main.go:12:22: undefined: gwdemo3.Bye Check the actual behavior with GOWORK=o f
  16. Use cases Multi Modules Monorepo ├── cmd │ ├── app1

    │ │ ├── go.mod │ │ └── main.go │ ├── app2 │ │ ├── go.mod │ │ └── main.go │ └── app3 │ ├── go.mod │ └── main.go ├── go.work └── pkg ├── echo │ ├── echo.go │ └── go.mod └── hello ├── go.mod └── hello.go