$30 off During Our Annual Pro Sale. View Details »

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. What Is the Go Workspace Mode
    Yuki Ito (@mrno110)
    Go 1.18 Release Party

    View Slide

  2. Merpay / Mercoin


    Architect


    Mercari


    Microservices Platform CI/CD
    Yuki Ito


    @mrno110

    View Slide

  3. Disclaimer
    $ go version


    go version go1.18rc1 linux/amd64
    As of this writing...

    View Slide

  4. Agenda
    ɾBackground


    ɾWorkspace Mode


    ɾUse cases

    View Slide

  5. Background
    ɾgithub.com/110y/gwdemo1


    ɾgithub.com/110y/gwdemo2


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

    View Slide

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

    View Slide

  7. Background
    gwdemo3
    package gwdemo3


    import "fmt"


    func Hello(s string) string {


    return fmt.Sprintf("Hello, %s!", s)


    }

    View Slide

  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"))


    }

    View Slide

  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)


    }

    View Slide

  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"))


    }

    View Slide

  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

    View Slide

  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.

    View Slide

  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

    View Slide

  14. Agenda
    ɾBackground


    ɾWorkspace Mode


    ɾUse cases

    View Slide

  15. Workspace Mode
    gwdemo3
    gwdemo2
    gwdemo1

    View Slide

  16. Workspace Mode
    Workspace
    gwdemo3
    gwdemo2
    gwdemo1

    View Slide

  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

    View Slide

  18. Workspace Mode
    Workspace
    $ go work init ./gwdemo1 ./gwdemo2 ./gwdemo3

    View Slide

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


    use (


    ./gwdemo1


    ./gwdemo2


    ./gwdemo3


    )
    go.work

    View Slide

  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

    View Slide

  21. Workspace Mode
    $ cd gwdemo1


    $ go env GOWORK


    /path/to/gwdemo-workspace/go.work
    Workspace

    View Slide

  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

    View Slide

  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

    View Slide

  24. Workspace Mode
    package main


    import (


    "fmt"


    "github.com/110y/gwdemo3"


    )


    func main() {


    fmt.Println(gwdemo3.Hello("Workspace"))


    fmt.Println(gwdemo3.Bye("Workspace"))


    }
    gwdemo1

    View Slide

  25. Workspace Mode
    $ cd gwdemo1


    $ go env GOWORK


    /path/to/gwdemo-workspace/go.work


    $ go run .


    Hello, Workspace!


    Bye, Workspace!
    Workspace

    View Slide

  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

    View Slide

  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

    View Slide

  28. Workspace Mode
    -work
    fi
    le
    fl
    ag...?
    https://go-review.googlesource.com/c/go/+/385995/

    View Slide

  29. Agenda
    ɾBackground


    ɾWorkspace Mode


    ɾUse cases

    View Slide

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

    View Slide

  31. Use cases
    chassis
    application
    template

    View Slide

  32. Use cases
    chassis
    application
    template
    Workspace

    View Slide

  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

    View Slide

  34. Workspace Mode
    Workspace
    gwdemo3
    gwdemo2
    gwdemo1

    View Slide