LoginSignup
0
5

More than 3 years have passed since last update.

サードパッケージを使用したGoのアプリケーションをHerokuにデプロイする v2

Last updated at Posted at 2020-02-29

以前書いた記事がGoのアップデートと共にゴミ記事と化したので別アプローチを記録
サードパッケージを使用したGoのアプリケーションをHerokuにデプロイする

結論から言うとgovendorをやめて、go modulesを使用する

Goでアプリケーションを作成

Goアプリケーションの作り方は前回の作り方とほぼ同じ

main.go
package main

import (
    "github.com/gin-gonic/gin"
)

type User struct {
    Name string
    Age  int
}

func main() {
    router := gin.Default()
    // css、js などの静的ファイルを読み込む場合。今回は使用しない。
    // router.Static("/assets", "./assets")

    router.LoadHTMLGlob("templates/*.html")

    router.GET("/", handler)

    router.Run()
}

func handler(ctx *gin.Context) {

    user := User{"User", 20}

    ctx.HTML(200, "index.html", gin.H{
        "user": user,
    })
}
templates/index.html
<!DOCTYPE html>
<html>
    <div>
        <p>Name: {{.user.Name}} </p>
        <p>Age: {{.user.Age}} </p>
    </div>
</html>

go modulesを使用してサードパッケージを追加

go modulesはGo 1.11が入っている場合は入っているそうですが、ない場合はinstallする

下記のサイトが参考になりました!
Go言語の依存モジュール管理ツール Modules の使い方

// 初期化
$ go mod init
go: creating new go.mod: module Heroku

go mod initが完了するとgo.modファイルが作成されます。

go.mod
module Heroku

go 1.13

初期化が完了したら、依存ファイルのダウンロードを行います。

// 依存ファイルをダウンロード
$ go get -v
Heroku

go.modファイルが更新され、新たにgo.sumファイルが作成されます。

go.mod
module Heroku

go 1.13

require github.com/gin-gonic/gin v1.5.0

go.sum
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
〜〜以下、依存ファイルがたくさん〜〜〜〜〜〜〜〜

作成したgo modluesをpushする

$ git add -A .
$ git commit -m "init"
[master 81579d9] init
 2 files changed, 44 insertions(+)
 create mode 100644 go.mod
 create mode 100644 go.sum

Herokuのアプリを作成→デプロイ
作成手順は以前のものと変わりません。

$ heroku create
Creating app... done, ⬢ pure-harbor-40656
https://pure-harbor-40656.herokuapp.com/ | https://git.heroku.com/pure-harbor-40656.git
$ git push heroku master

でけた

所感

今回の対応はGoと使用しているサードパッケージのアップデートによる影響でした。(詳細は下記記事)
govendorで依存関係のファイルが取得できないエラーについて

今回のケースから
技術者は変わっていく技術にちゃんとキャッチアップしていかなきゃ行けない。
調べる時の単語のチョイス、範囲、絞り方がとても重要だなと感じました。

後、英語はちゃんと勉強しようね

参考サイト

Go言語の依存モジュール管理ツール Modules の使い方
サードパッケージを使用したGoのアプリケーションをHerokuにデプロイする

0
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
5