Golang から「Cloud Datastore エミュレータ」に対して接続ができない


Cloud Datastore を試してみようと思って、Mac で開発環境を作っていたのですが些細なことでハマったので備忘録として記憶しておきます。

サンプル通りにエミュレータを起動して、サンプルコードを書いたけど datastore へ繋がらない状態だったんですが、datasotre のエミュレーター起動時にホスト名を指定（localhost）することで解決することができました。
基本はマニュアル（[Cloud Datastore エミュレータの実行](https://cloud.google.com/datastore/docs/tools/datastore-emulator)）にある通り実行してます。

# 環境
* macOS Catalina 10.15.1
* Google Cloud SDK 272.0.0
* app-engine-go 
* beta 2019.05.17
* cloud-datastore-emulator 2.1.0
* core 2019.11.16

# 実行結果
## ホスト指定前
```sh
% sudo gcloud beta emulators datastore start
WARNING: Reusing existing data in [/Users/xxxxxx/.config/gcloud/emulators/datastore].
Executing: /Applications/local/google-cloud-sdk/platform/cloud-datastore-emulator/cloud_datastore_emulator start --host=::1 --port=8173 --store_on_disk=True --consistency=0.9 --allow_remote_shutdown /Users/xxxxxx/.config/gcloud/emulators/datastore
[datastore] 12 04, 2019 3:07:43 午後 com.google.cloud.datastore.emulator.CloudDatastore$FakeDatastoreAction$9 apply
[datastore] 情報: Provided --allow_remote_shutdown to start command which is no longer necessary.
[datastore] 12 04, 2019 3:07:43 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub <init>
[datastore] 情報: Local Datastore initialized:
[datastore] 	Type: High Replication
[datastore] 	Storage: /Users/xxxxxx/.config/gcloud/emulators/datastore/WEB-INF/appengine-generated/local_db.bin
[datastore] 12 04, 2019 3:07:44 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub load
[datastore] 情報: Time to load datastore: 55 ms
[datastore] API endpoint: http://::1:8173
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=::1:8173
[datastore] 
[datastore] Dev App Server is now running.
[datastore] 
[datastore] The previous line was printed for backwards compatibility only.
[datastore] If your tests rely on it to confirm emulator startup,
[datastore] please migrate to the emulator health check endpoint (/). Thank you!
```
```sh
% gcloud beta emulators datastore env-init
export DATASTORE_DATASET=xxxxx
export DATASTORE_EMULATOR_HOST=::1:8173
export DATASTORE_EMULATOR_HOST_PATH=::1:8173/datastore
export DATASTORE_HOST=http://::1:8173
export DATASTORE_PROJECT_ID= xxxxx
```

## ホスト指定後
```sh
% sudo gcloud beta emulators datastore start --host-port localhost:8173
WARNING: Reusing existing data in [/Users/xxxxxx/.config/gcloud/emulators/datastore].
Executing: /Applications/local/google-cloud-sdk/platform/cloud-datastore-emulator/cloud_datastore_emulator start --host=localhost --port=8173 --store_on_disk=True --consistency=0.9 --allow_remote_shutdown /Users/xxxxxx/.config/gcloud/emulators/datastore
[datastore] 12 04, 2019 3:07:43 午後 com.google.cloud.datastore.emulator.CloudDatastore$FakeDatastoreAction$9 apply
[datastore] 情報: Provided --allow_remote_shutdown to start command which is no longer necessary.
[datastore] 12 04, 2019 3:11:34 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub <init>
[datastore] 情報: Local Datastore initialized:
[datastore] 	Type: High Replication
[datastore] 	Storage: /Users/xxxxxx/.config/gcloud/emulators/datastore/WEB-INF/appengine-generated/local_db.bin
[datastore] 12 04, 2019 3:11:35 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub load
[datastore] 情報: Time to load datastore: 55 ms
[datastore] API endpoint: http://localhost:8173
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=localhost:8173
[datastore] 
[datastore] Dev App Server is now running.
[datastore] 
[datastore] The previous line was printed for backwards compatibility only.
[datastore] If your tests rely on it to confirm emulator startup,
[datastore] please migrate to the emulator health check endpoint (/). Thank you!
```
```sh
% gcloud beta emulators datastore env-init
export DATASTORE_DATASET=xxxxx
export DATASTORE_EMULATOR_HOST=localhost:8173
export DATASTORE_EMULATOR_HOST_PATH=localhost:8173/datastore
export DATASTORE_HOST=http://localhost:8173
export DATASTORE_PROJECT_ID= xxxxx
```

# 原因
一応、解決はしたのですが他の人も発生していないしもう少し調べてみました。
[エミュレーターのマニュアル](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/datastore/start)によると、`--host-port`の初期値が`localhost:8081`になっていました。
![Kobito.dCvm95.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/203864/6501fa8e-331c-adf7-e0a2-7f7176ff0fc1.png "Kobito.dCvm95.png")


ゾンビプロセスが動いてた。。。

```sh
 % lsof -i4TCP:8081
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    33338 xxxx   45u  IPv6 0xf139f401fffec995      0t0  TCP localhost:sunproxyadmin (LISTEN)
```

ゾンビプロセスを削除して、もう1回エミューレーターを起動してみたらちゃんと`localhost`で起動されました。

```sh
% sudo gcloud beta emulators datastore start                                                  
WARNING: Reusing existing data in [/Users/xxxxxx/.config/gcloud/emulators/datastore].
Executing: /Applications/local/google-cloud-sdk/platform/cloud-datastore-emulator/cloud_datastore_emulator start --host=localhost --port=8081 --store_on_disk=True --consistency=0.9 --allow_remote_shutdown /Users/xxxxxx/.config/gcloud/emulators/datastore
[datastore] 12 04, 2019 3:19:20 午後 com.google.cloud.datastore.emulator.CloudDatastore$FakeDatastoreAction$9 apply
[datastore] 情報: Provided --allow_remote_shutdown to start command which is no longer necessary.
[datastore] 12 04, 2019 3:19:20 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub <init>
[datastore] 情報: Local Datastore initialized:
[datastore] 	Type: High Replication
[datastore] 	Storage: /Users/xxxxxx/.config/gcloud/emulators/datastore/WEB-INF/appengine-generated/local_db.bin
[datastore] 12 04, 2019 3:19:20 午後 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub load
[datastore] 情報: Time to load datastore: 55 ms
[datastore] API endpoint: http://localhost:8081
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=localhost:8081
[datastore] 
[datastore] Dev App Server is now running.
[datastore] 
[datastore] The previous line was printed for backwards compatibility only.
[datastore] If your tests rely on it to confirm emulator startup,
[datastore] please migrate to the emulator health check endpoint (/). Thank you!
```

別プロセスで`8081`ポートを既に開発環境で利用されてる方は`--host-port`オプションを指定してお試しください。