HerokuにGolangのアプリをデプロイする

#はじめに
HerokuにGolangのアプリをデプロイする。

#環境情報
PC：Mac(CPUはintel製)
Go：1.17.6 ←Goのver大事。versionは1.16以降をインストールしてください。
開発エディタ：Visual Studio Code

#環境設定
HerokuCLI、GitCLIを利用するため、まだの人は下記からインストールしてください。また、アカウント作成も同時に済ませてください。

○Git CLI
[https://git-scm.com/book/en/v2/Getting-Started-Installing-Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
○Heroku CLI
[https://devcenter.heroku.com/ja/articles/heroku-cli](https://devcenter.heroku.com/ja/articles/heroku-cli)

#Herokuアプリ作成
```shell:herokuにログイン
heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
```

```shell:Golangアプリケーション（サンプル）のダウンロード
git clone https://github.com/heroku/go-getting-started.git
cd go-getting-started
```

#ローカル実行
```shell:ローカル実行(失敗)
~/go/src/go-getting-started $ go run main.go
2022/02/11 17:37:35 $PORT must be set
exit status 1
```
→環境変数にPORTが設定されていないことが問題。下記コマンドで環境変数を設定

```shell:環境変数設定
~/go/src/go-getting-started $ export PORT=3000
```

```shell:ローカル実行（成功）
~/go/src/go-getting-started $ go run main.go  
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] Loaded HTML Templates (4): 
        - 
        - header.tmpl.html
        - index.tmpl.html
        - nav.tmpl.html

[GIN-debug] GET    /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (2 handlers)
[GIN-debug] HEAD   /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (2 handlers)
[GIN-debug] GET    /                         --> main.main.func1 (2 handlers)
[GIN-debug] Listening and serving HTTP on :3000
```
[http://localhost:3000](http://localhost:3000)で実行。下記画面が出ればOK！！！

![スクリーンショット 2022-02-11 17.43.03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/d6c41caf-1dd4-9ce8-6509-ee9b55a84880.png)

#サーバ実行
```shell:gitのローカルリポジトリcommit
~/go/src/go-getting-started $ git add ./                                       
~/go/src/go-getting-started $ git commit -m "initial commit" 
[main c71285d] initial commit
 1 file changed, 1 insertion(+), 1 deletion(-)
```

```shell:herokuアプリ作成
heroku create
Creating polar-inlet-4930... done, stack is heroku-18
https://polar-inlet-4930.herokuapp.com/ | https://git.heroku.com/polar-inlet-4930.git
Git remote heroku added
```

```shell:herokuへデプロイ
git push heroku main
Enumerating objects: 521, done.
Counting objects: 100% (521/521), done.
Delta compression using up to 8 threads
Compressing objects: 100% (309/309), done.
Writing objects: 100% (521/521), 226.26 KiB | 45.25 MiB/s, done.
Total 521 (delta 141), reused 501 (delta 134)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Go app detected
remote: -----> Fetching jq... done
remote:
remote: -----> Detected go modules - go.mod
remote:
remote: -----> Installing go1.12.1
remote: -----> Fetching go1.12.1.linux-amd64.tar.gz... done
remote:  !!    Installing package '.' (default)
remote:  !!
remote:  !!    To install a different package spec add a comment in the following form to your `go.mod` file:
remote:  !!    // +heroku install ./cmd/...
remote:  !!
remote: -----> Running: go install -v -tags heroku -mod=vendor .
remote: gopkg.in/bluesuncorp/validator.v5
remote: github.com/gin-gonic/gin/render
remote: github.com/manucorporat/sse
remote: github.com/mattn/go-colorable
remote: golang.org/x/net/context
remote: github.com/heroku/x/hmetrics
remote: github.com/heroku/x/hmetrics/onload
remote: github.com/gin-gonic/gin/binding
remote: github.com/gin-gonic/gin
remote: github.com/heroku/go-getting-started
remote:
remote: Compiled the following binaries:
remote:        ./bin/go-getting-started
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 5.5M
remote: -----> Launching...
remote:        Released v3
remote:        https://go-on-heroku.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/go-on-heroku.git
 * [new branch]      main -> main
```

heroku openでブラウザが開き、ローカルと同じ画面が出ればOKです！！！


#参考URL
https://devcenter.heroku.com/ja/articles/getting-started-with-go?singlepage=true