[自分用メモ] はじめてのGoogle Cloud Functions(Go言語編)
3486 ワード
注意
ここに書かれていることは、すべて下記の公式ドキュメントに書かれているものです。このブログは、あくまで自分用メモになります。
公式ドキュメント → https://cloud.google.com/functions/docs
Google Cloud FunctionsとGo
Goが使えるようになったのは2019年1月(参考記事)。
2020年9月現在、使えるGoのバージョンは1.11と1.13のみ(参考記事)。
関数を作る
ローカルの開発環境に、適当に次のような関数を作る。
hello.go
package hello
import (
"fmt"
"net/http"
)
func Hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
外部ライブラリとかを使いたければ、go.modを書けば良い。
GCPプロジェクト作成
適当にプロジェクトを作っておく。
ここに書いてある「APIの有効化」とかが必要かも。
gcloudコマンドでデプロイ
事前準備
# ログイン
$ gcloud auth login
# 操作対象のプロジェクトを設定
$ gcloud config set project プロジェクトID
関数のデプロイ
$ gcloud functions deploy Hello --runtime go113 --trigger-http --allow-unauthenticated
事前準備
# ログイン
$ gcloud auth login
# 操作対象のプロジェクトを設定
$ gcloud config set project プロジェクトID
関数のデプロイ
$ gcloud functions deploy Hello --runtime go113 --trigger-http --allow-unauthenticated
Hello
の部分は、Goで書いた関数名と揃える必要があるっぽい。
実行
先ほどの gcloud functions
コマンドの結果に、次のようなものが含まれている。このURLにアクセスすればよい。
httpsTrigger:
url: https://us-central1-my-first-function-xxxxxx.cloudfunctions.net/Hello
Author And Source
この問題について([自分用メモ] はじめてのGoogle Cloud Functions(Go言語編)), 我々は、より多くの情報をここで見つけました https://qiita.com/suke_masa/items/b10aa0f1f7ab665b8903著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .