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

What Is the Go Workspace Mode

1bfc6e2ed04a895bb36f36b86828b689?s=47 Yuki Ito
February 18, 2022

What Is the Go Workspace Mode

1bfc6e2ed04a895bb36f36b86828b689?s=128

Yuki Ito

February 18, 2022
Tweet

More Decks by Yuki Ito

Other Decks in Programming

Transcript

  1. What Is the Go Workspace Mode Yuki Ito (@mrno110) Go

    1.18 Release Party
  2. Merpay / Mercoin Architect Mercari Microservices Platform CI/CD Yuki Ito

    @mrno110
  3. Disclaimer $ go version go version go1.18rc1 linux/amd64 As of

    this writing...
  4. Agenda ɾBackground ɾWorkspace Mode ɾUse cases

  5. Background ɾgithub.com/110y/gwdemo1 ɾgithub.com/110y/gwdemo2 ɾgithub.com/110y/gwdemo3 Let's assume we develop 3 modules

  6. Background gwdemo3 gwdemo2 gwdemo1 Let's assume we develop 3 modules

  7. Background gwdemo3 package gwdemo3 import "fmt" func Hello(s string) string

    { return fmt.Sprintf("Hello, %s!", s) }
  8. 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")) }
  9. 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) }
  10. 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")) }
  11. 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
  12. Background Problem ɾHave to add replace directive to each module's

    go.mod fi le. ɾHave to remove replace directive before submitting the code.
  13. 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
  14. Agenda ɾBackground ɾWorkspace Mode ɾUse cases

  15. Workspace Mode gwdemo3 gwdemo2 gwdemo1

  16. Workspace Mode Workspace gwdemo3 gwdemo2 gwdemo1

  17. 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
  18. Workspace Mode Workspace $ go work init ./gwdemo1 ./gwdemo2 ./gwdemo3

  19. Workspace Mode go work init creates go.work fi le go

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

    │ ├── go.mod │ ├── go.sum │ └── main.go ├── gwdemo2 │ ├── go.mod │ ├── go.sum │ └── main.go └── gwdemo3 ├── go.mod └── gwdemo3.go
  21. Workspace Mode $ cd gwdemo1 $ go env GOWORK /path/to/gwdemo-workspace/go.work

    Workspace
  22. 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
  23. 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
  24. Workspace Mode package main import ( "fmt" "github.com/110y/gwdemo3" ) func

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

    $ go run . Hello, Workspace! Bye, Workspace! Workspace
  26. 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
  27. 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
  28. Workspace Mode -work fi le fl ag...? https://go-review.googlesource.com/c/go/+/385995/

  29. Agenda ɾBackground ɾWorkspace Mode ɾUse cases

  30. Use cases Microservice Chassis Pattern https://microservices.io/patterns/microservice-chassis.html

  31. Use cases chassis application template

  32. Use cases chassis application template Workspace

  33. 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
  34. Workspace Mode Workspace gwdemo3 gwdemo2 gwdemo1