GAE(Google App Engine) で Golang を開発するための環境を構築する #golang


前回の記事（[Golang と Line Notify を利用して API の証明書期限切れチェック](https://qiita.com/ynozue/items/d00a76e16d5a3d5a784c)）でも投稿しましたが、Golang を勉強するために色々やってみようと思っています。
今回は Google App Engine を利用して Golang が動く環境構築まで行おうと思います。
※ 簡単な Web サービスを GAE 上で動かせたらゴール


それにしても、5年くらい前？に触った時は Python と Java しかなかったのに今はすごい増えてますし Google Cloud Platform に統合されてるんですね。
※ 対応言語： Node.js、Java、Ruby、C#、Go、Python、PHP

# こんな順番で進めます
1. 「Google Cloud SDK」をダウンロード&インストール
1. Golang 用 Plugin の「App Engine Go Extensions」をインストール
1. 「App Engine Go Extensions」Plugin を利用しやすいように Alias を設定 


# 実行ログ
##「Google Cloud SDK」をダウンロード&インストール
###ダウンロード
```sh
$ curl https://sdk.cloud.google.com | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   443    0   443    0     0    914      0 --:--:-- --:--:-- --:--:--   915
Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash
######################################################################## 100.0%
Running install script from: /tmp/tmp.rf4IlNzBX6/install_google_cloud_sdk.bash
which curl
curl -# -f https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz
######################################################################## 100.0%
```
###インストール先を選択
```sh
Installation directory (this will create a google-cloud-sdk subdirectory) (/root): /usr/local

mkdir -p /usr/local
tar -C /usr/local -zxvf /tmp/tmp.Dn6wT8uD8o/google-cloud-sdk.tar.gz
google-cloud-sdk/
google-cloud-sdk/lib/
google-cloud-sdk/lib/third_party/
…
google-cloud-sdk/.install/.download/

/usr/local/google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!
WARNING: You appear to be running this script as root. This may cause 
the installation to be inaccessible to users other than the root user.

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true
```

###SDKの改善に協力するか聞かれたので、今回は協力（Y）するを選択
```sh
Do you want to help improve the Google Cloud SDK (Y/n)?  Y


This will install all the core command line tools necessary for working with
the Google Cloud Platform.



Your current Cloud SDK version is: 173.0.0
Installing components from version: 173.0.0

┌────────────────────────────────────────────────────────────────────────────┐
│                    These components will be installed.                     │
├─────────────────────────────────────────────────────┬────────────┬─────────┤
│                         Name                        │  Version   │   Size  │
├─────────────────────────────────────────────────────┼────────────┼─────────┤
│ BigQuery Command Line Tool                          │     2.0.26 │ < 1 MiB │
│ BigQuery Command Line Tool (Platform Specific)      │     2.0.26 │ < 1 MiB │
│ Cloud SDK Core Libraries (Platform Specific)        │ 2017.09.15 │ < 1 MiB │
│ Cloud Storage Command Line Tool                     │       4.27 │ 3.0 MiB │
│ Cloud Storage Command Line Tool (Platform Specific) │       4.27 │ < 1 MiB │
│ Default set of gcloud commands                      │            │         │
│ gcloud cli dependencies                             │ 2017.09.15 │ 5.1 MiB │
└─────────────────────────────────────────────────────┴────────────┴─────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool                   ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Default set of gcloud commands               ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.                                                                                                                                                                                                                                             

Update done!
```
###プロファイルを変更して補完を有効にするか聞かれたので、今回は協力（Y）するを選択
```sh
Modify profile to update your $PATH and enable shell command 
completion?

Do you want to continue (Y/n)? Y

The Google Cloud SDK installer will now prompt you to update an rc 
file to bring the Google Cloud CLIs into your environment.
```
###利用ユーザーのプロファイルを指定
```sh
Enter a path to an rc file to update, or leave blank to use 
[/root/.bashrc]:  /home/vagrant/.bashrc

Backing up [/home/vagrant/.bashrc] to [/home/vagrant/.bashrc.backup].
[/home/vagrant/.bashrc] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts
```
###プロファイルを再読み込み
```sh
source ~/.bashrc
```
###インストールしたコンポーネントを確認
```sh
$ gcloud components list

Your current Cloud SDK version is: 173.0.0
The latest available version is: 173.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                  Components                                                 │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│     Status    │                         Name                         │            ID            │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go            │  98.0 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                      │   4.1 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                 │   3.5 MiB │
│ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  15.4 MiB │
│ Not Installed │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  33.2 MiB │
│ Not Installed │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed │ Google Container Local Builder                       │ container-builder-local  │   3.7 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   2.2 MiB │
│ Not Installed │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 130.9 MiB │
│ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php           │           │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python        │   6.3 MiB │
│ Not Installed │ kubectl                                              │ kubectl                  │  16.0 MiB │
│ Installed     │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Installed     │ Cloud SDK Core Libraries                             │ core                     │   6.8 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │   3.0 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [173.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [173.0.0], run:
  $ gcloud components update
```

##Golang 用 Plungin の「App Engine Go Extensions」をインストール
###Golang 用 Plungin の「App Engine Go Extensions」をインストール
```sh
$ sudo /usr/local/google-cloud-sdk/bin/gcloud components install app-engine-go


Your current Cloud SDK version is: 173.0.0
Installing components from version: 173.0.0

┌───────────────────────────────────────────────────┐
│        These components will be installed.        │
├──────────────────────────────┬─────────┬──────────┤
│             Name             │ Version │   Size   │
├──────────────────────────────┼─────────┼──────────┤
│ App Engine Go Extensions     │  1.9.59 │ 98.0 MiB │
│ App Engine Go Extensions     │         │          │
│ gcloud app Python Extensions │  1.9.60 │  6.3 MiB │
└──────────────────────────────┴─────────┴──────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  Y


╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: App Engine Go Extensions                     ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: App Engine Go Extensions                     ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: gcloud app Python Extensions                 ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...
..............................................................done.

Update done!
```
###「App Engine Go Extensions」がインストールされたか確認
```sh
$ gcloud components list

Your current Cloud SDK version is: 173.0.0
The latest available version is: 173.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                  Components                                                 │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│     Status    │                         Name                         │            ID            │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                      │   4.1 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                 │   3.5 MiB │
│ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  15.4 MiB │
│ Not Installed │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  33.2 MiB │
│ Not Installed │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed │ Google Container Local Builder                       │ container-builder-local  │   3.7 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   2.2 MiB │
│ Not Installed │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 130.9 MiB │
│ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php           │           │
│ Not Installed │ kubectl                                              │ kubectl                  │  16.0 MiB │
│ Installed     │ App Engine Go Extensions                             │ app-engine-go            │  98.0 MiB │
│ Installed     │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Installed     │ Cloud SDK Core Libraries                             │ core                     │   6.8 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │   3.0 MiB │
│ Installed     │ gcloud app Python Extensions                         │ app-engine-python        │   6.3 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [173.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [173.0.0], run:
  $ gcloud components update
```
###「Google Cloud SDK」を最新の状態にアップデートがないか確認
```sh
$ sudo /usr/local/google-cloud-sdk/bin/gcloud components update

All components are up to date.
```
##「App Engine Go Extensions」Plugin を利用しやすいように Alias を設定
###実行権限を付与
```sh
$ sudo chmod 755 /usr/local/google-cloud-sdk/platform/google_appengine/goapp
```
###プロファイルに Alias を追加
```sh
echo "alias goapp=/usr/local/google-cloud-sdk/platform/google_appengine/goapp" >> ~/.bashrc
```
###プロファイルを再読み込み
```sh
source ~/.bashrc
```
###「goapp」コマンドが利用できることを確認
```sh
$ goapp env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/google-cloud-sdk/platform/google_appengine/gopath"
GORACE=""
GOROOT="/usr/local/google-cloud-sdk/platform/google_appengine/goroot-1.6"
GOTOOLDIR="/usr/local/google-cloud-sdk/platform/google_appengine/goroot-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
```