Deploy Go applications¶
Overview¶
A Go app on tsuru needs go.mod, your .go files and optionally a Procfile and tsuru.yaml.
The go platform downloads the toolchain, runs go mod download and builds your
binaries at deploy time.
Creating the app¶
tsuru app create helloworld go
Application code¶
A minimal main.go:
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", hello)
http.HandleFunc("/healthcheck", healthcheck)
port := os.Getenv("PORT")
if port == "" {
port = "8888"
}
if err := http.ListenAndServe(":"+port, nil); err != nil {
panic(err)
}
}
func hello(res http.ResponseWriter, req *http.Request) {
fmt.Fprintln(res, "hello, world!")
}
func healthcheck(res http.ResponseWriter, req *http.Request) {
fmt.Fprintln(res, "WORKING")
}
Create go.mod (replace the module path with your repository):
go mod init github.com/tsuru/helloworld
go mod tidy
And a tsuru.yaml, see tsuru.yaml:
healthcheck:
path: /healthcheck
Go version¶
The toolchain version is chosen as follows:
- the
GO_VERSIONenvironment variable, if set (tsuru env set -a helloworld GO_VERSION=1.23); - otherwise the
godirective ingo.mod.
An exact match of a published release is used when it exists. Otherwise the newest stable
release with that prefix is used (1.23 picks the latest 1.23.x; 1.23.x works too). If nothing
matches, the latest stable Go is installed and the deploy log says so.
Build and Procfile¶
- Without a
Procfilethe platform runsgo build -o tsuru-appin the project root and uses its default Procfile,web: ./tsuru-app. - With a
Procfileit runsgo install ./..., so everymainpackage under the module ends up onPATHand the Procfile refers to binaries by name (see below). - A
vendor/directory switches the build to-mod=vendor.
Deploy¶
tsuru app deploy -a helloworld .
tsuru compiles and starts the application; tsuru app info -a helloworld shows its address.
Multiple processes¶
Teams often split an application into an API and a worker that share one repository. Put one
main package per binary under cmd/:
cmd/myapp-api/main.go # builds the myapp-api binary
cmd/myapp-worker/main.go # builds the myapp-worker binary
Shared packages live outside cmd/. Reference each binary in the Procfile:
web: myapp-api
worker: myapp-worker
A full working example is in
tsuru/platforms examples/go.