Go

Last updated 3rd June 2021

Objective

Web PaaS supports building and deploying applications written in Go using Go modules. They are compiled during the Build hook phase, and support both committed dependencies and download-on-demand.

Supported versions

Grid
1.11
1.12
1.13
1.14
1.15

To specify a Go container, use the type property in your .platform.app.yaml.

type: 'golang:1.15'

Deprecated versions

The following container versions are also available. However, due to their lack of Go module support and the difficulties in supporting the GOPATH during the Web PaaS build they are not recommended.

  • 1.10
  • 1.8
  • 1.9

Go modules

The recommended way to handle Go dependencies on Web PaaS 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.)

Web PaaS variables

Web PaaS exposes relationships and other configuration as environment variables. To make them easier to access you should use the provided Config Reader library. Most notably, it allows a program to determine at runtime what HTTP port it should listen on and what the credentials are to access other services.

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 Web PaaS Environment.")
    }

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

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

Building and running the application

Assuming your go.mod and go.sum files are present in your repository, the application may be built with a simple go build command that will 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. Should the program terminate for any reason it will be automatically restarted.

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

name: app

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.
            # If you want some files served directly by the web server without hitting Go, see
            # https://docs.ovh.com/gb/en/web-paas/configuration-app/web/
            allow: false
            passthru: true

disk: 1024

Note that there will still be 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.

Project templates

Web PaaS offers a project templates for Go applications using the structure described above. It can be used as a starting point or reference for building your own website or web application.

Hugo

image

This template provides a basic Hugo skeleton. All files are generated at build time, so at runtime only static files need to be served. The Hugo executable itself is downloaded during the build hook. You can specify the version to use by updating the `.platform.app.yaml` file. It also includes a minimal template to get you started, but you are free to replace it with your own template.

Hugo is a static site generator written in Go, using Go's native template packages for formatting.

Features

  • Go 1.15
  • Automatic TLS certificates
  • Hugo downloaded on the fly during build

View the repository on GitHub.

Basic Go

image

This template provides the most basic configuration for running a custom Go project using Go modules. It demonstrates the Web PaaS `config-reader` library and connecting to a MariaDB instance. It can be used to build a very rudimentary application but is intended primarily as a documentation reference.

Go is a statically typed, compiled language with an emphasis on easy concurrency and network services.

Features

  • Go 1.15
  • MariaDB 10.4
  • Automatic TLS certificates
  • Git module-based build

View the repository on GitHub.

Beego

image

This template demonstrates building the Beego framework for Web PaaS using Go modules. It includes a minimalist application skeleton that demonstrates how to connect to a MariaDB server. It is intended for you to use as a starting point and modify for your own needs.

Beego is a popular web framework written in Go.

Features

  • Go 1.14
  • MariaDB 10.4
  • Automatic TLS certificates
  • Git module-based build

View the repository on GitHub.

Echo

image

This template demonstrates building the Echo framework for Web PaaS using Go modules. It includes a minimalist application skeleton that demonstrates how to connect to a MariaDB server. It is intended for you to use as a starting point and modify for your own needs.

Echo is a lightweight, minimalist web framework written in Go.

Features

  • Go 1.14
  • MariaDB 10.4
  • Automatic TLS certificates
  • Git module-based build

View the repository on GitHub.

Gin

image

This template demonstrates building the Gin framework for Web PaaS using Go modules. It includes a minimalist application skeleton that demonstrates how to connect to a MariaDB server for data storage. It is intended for you to use as a starting point and modify for your own needs.

Gin is a lightweight web framework written in Go that emphasizes performance.

Features

  • Go 1.14
  • MariaDB 10.4
  • Git module-based build

View the repository on GitHub.


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices exclude VAT. VAT may vary according to the customer's country of residence.