Go Workspaces

Workspaces

Go 1.18 introduced workspace support. A workspace lets you manage multiple modules together.

  1. Create a workspace.

    $ mkdir workspace
    $ cd workspace
    $ go work init
    
  2. Create the myapp module.

    $ mkdir myapp
    $ cd myapp
    $ go mod init example.com/myapp
    
  3. Add the myapp module to the workspace.

    $ cd ..
    $ go work use ./myapp
    

    myapp.go

    package main
    
    import "fmt"
    import "example.com/mypkg"
    
    func main() {
        fmt.Println(mypkg.Hello())
    }
    
  4. Create the mypkg module.

    $ mkdir mypkg
    $ cd mypkg
    $ go mod init example.com/mypkg
    
  5. Add the mypkg module to the workspace.

    $ cd ..
    $ go work use ./mypkg
    

    mypkg.go

    package mypkg
    
    func Hello() string {
        return "Hello world!"
    }
    
  6. Run the program.

    $ go run example.com/myapp
    Hello world!