Platform.sh User Documentation

Go

Platform.sh supports building and deploying applications written in Go using Go modules. They’re compiled during the Build hook phase, and support both committed dependencies and download-on-demand.

Supported versions Anchor to this heading

You can select the major and minor version.

Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.

Grid Dedicated Gen 3 Dedicated Gen 2
  • 1.24
  • 1.23
  • 1.22
  • 1.21
  • 1.20
None available None available

Specify the language Anchor to this heading

To use Go, specify golang as your app’s type:

.platform.app.yaml
type: 'golang:<VERSION_NUMBER>'

For example:

.platform.app.yaml
type: 'golang:1.24'

Deprecated versions Anchor to this heading

The following versions are deprecated. They’re available, but they aren’t receiving security updates from upstream and aren’t guaranteed to work. They’ll be removed in the future, so migrate to one of the supported versions.

  • 1.19
  • 1.18
  • 1.17
  • 1.16
  • 1.15
  • 1.14
  • 1.13
  • 1.12
  • 1.11
  • 1.10
  • 1.9
  • 1.8

Go toolchain auto download Anchor to this heading

Even though you select a specific version of Go, starting in Go 1.21, the go command will automatically download and use a different toolchain version as requested by the toolchain directive in go.mod, or the GOTOOLCHAIN environmental variable (see Go Toolchains).

Go modules Anchor to this heading

The recommended way to handle Go dependencies on Platform.sh is using Go module support in Go 1.11 and later. That allows the build process to use go build directly without any extra steps, and you can specify an output executable file of your choice. (See the examples below.)

Building and running the application Anchor to this heading

Assuming your go.mod and go.sum files are present in your repository, your application can be built with the command go build, to produce a working executable. You can then start it from the web.commands.start directive. Note that the start command must run in the foreground. If the program terminates for any reason it is automatically restarted.

The following basic .platform.app.yaml file is sufficient to run most Go applications.

.platform.app.yaml
name: myapp

type: golang:1.14

hooks:
  build: |
    # Modify this line if you want to build differently or use an alternate name for your executable.
    go build -o bin/app    

web:
  upstream:
    socket_family: tcp
    protocol: http

  commands:
    # If you change the build output in the build hook above, update this line as well.
    start: ./bin/app

  locations:
    /:
      # Route all requests to the Go app, unconditionally.
      allow: false
      passthru: true

disk: 1024

Note that there is still an Nginx proxy server sitting in front of your application. If desired, certain paths may be served directly by Nginx without hitting your application (for static files, primarily) or you may route all requests to the Go application unconditionally, as in the example above.

Accessing services Anchor to this heading

To access various services with Go, see the following examples. The individual service pages have more information on configuring each service.

package examples

import (
	"fmt"
	"github.com/bradfitz/gomemcache/memcache"
	psh "github.com/platformsh/config-reader-go/v2"
	gomemcache "github.com/platformsh/config-reader-go/v2/gomemcache"
)

func UsageExampleMemcached() string {

	// Create a NewRuntimeConfig object to ease reading the Platform.sh environment variables.
	// You can alternatively use os.Getenv() yourself.
	config, err := psh.NewRuntimeConfig()
	checkErr(err)

	// Get the credentials to connect to the Solr service.
	credentials, err := config.Credentials("memcached")
	checkErr(err)

	// Retrieve formatted credentials for gomemcache.
	formatted, err := gomemcache.FormattedCredentials(credentials)
	checkErr(err)

	// Connect to Memcached.
	mc := memcache.New(formatted)

	// Set a value.
	key := "Deploy_day"
	value := "Friday"

	err = mc.Set(&memcache.Item{Key: key, Value: []byte(value)})

	// Read it back.
	test, err := mc.Get(key)

	return fmt.Sprintf("Found value <strong>%s</strong> for key <strong>%s</strong>.", test.Value, key)
}

You can get all information about a deployed environment, including how to connect to services, through environment variables. Your app can access these variables.

You can also use the library to read other environment variables.

package main

import (
	_ "github.com/go-sql-driver/mysql"
	psh "github.com/platformsh/gohelper"
	"net/http"
)

func main() {

	p, err := psh.NewPlatformInfo()

	if err != nil {
		panic("Not in a Platform.sh Environment.")
	}

	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
		// ...
	})

	http.ListenAndServe(":"+p.Port, nil)
}

Configuration reader Anchor to this heading

While you can read the environment directly from your app, you might want to use the Go configuration reader library. It decodes service credentials, the correct port, and other information for you.

Project templates Anchor to this heading

The following list shows templates available for Go apps.

A template is a starting point for building your project. It should help you get a project ready for production.