高木のブログ

「Hello World!」を返すGo言語アプリをHerokuにデプロイする

2019/11/23

概要

「Hello World!」を返すGo言語アプリをHerokuにデプロイする。
以下に載っているチュートリアルをそのままやるだけ。
https://jp.heroku.com/go

バージョン

$ go version
go version go1.12.9 darwin/amd64

$ govendor -version
v1.0.9

$ heroku --version
heroku/7.35.0 darwin-x64 node-v12.13.0

手順

Govendorのインストール

パッケージ管理ツールを入れる。RubyでいうBundlerかな。

$ go get -u github.com/kardianos/govendor

kardianos/govendor - GitHub

「Hello World! 」と応答するコマンド(world)を作成

$ mkdir hello_golang_on_heroku
$ cd hello_golang_on_heroku
$ vim main.go
main.go

package main

import (
    "io"
    "net/http"
    "os"
)

func hello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello World!")
}

func main() {
    port := os.Getenv("PORT")
    http.HandleFunc("/", hello)
    http.ListenAndServe(":"+port, nil)
}

govendor init を使ってマニフェストを生成

$ govendor init
$ tree .
.
├── main.go
└── vendor
    └── vendor.json

heroku create を使ってアプリをプロビジョン

$ heroku create
Creating app... done, ⬢ shrouded-taiga-69918
https://shrouded-taiga-69918.herokuapp.com/ | https://git.heroku.com/shrouded-taiga-69918.git

git push heroku master を使ってデプロイ

$ git push heroku master
Counting objects: 7, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 791 bytes | 791.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Go app detected
remote: -----> Fetching jq... done
remote: -----> Fetching stdlib.sh.v8... done
remote: -----> Checking vendor/vendor.json file.
remote:  !!    The 'heroku.goVersion' field is not specified in 'vendor/vendor.json'.
remote:  !!
remote:  !!    Defaulting to go1.12.12
remote:  !!
remote:  !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-govendor#build-configuration
remote:  !!
remote: -----> New Go Version, clearing old cache
remote: -----> Installing go1.12.12
remote: -----> Fetching go1.12.12.linux-amd64.tar.gz... done
remote: -----> Fetching govendor... done
remote:  !!    Installing package '.' (default)
remote:  !!
remote:  !!    To install a different package spec set 'heroku.install' in 'vendor/vendor.json'
remote:  !!
remote:  !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-govendor#build-configuration
remote:  !!
remote: -----> Fetching any unsaved dependencies (govendor sync)
remote: -----> Running: go install -v -tags heroku .
remote: github.com/ytkg/hello_golang_on_heroku
remote:
remote:        Installed the following binaries:
remote:                ./bin/hello_golang_on_heroku
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 3.6M
remote: -----> Launching...
remote:        Released v3
remote:        https://shrouded-taiga-69918.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/shrouded-taiga-69918.git
* [new branch]      master -> master

動作確認

$ curl https://shrouded-taiga-69918.herokuapp.com
Hello World!

リポジトリ

ytkg/hello_golang_on_heroku - GitHub

参考

Go アプリをクラウドにデプロイ・運用・スケール | Heroku


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub