cloud Functions

Where to start?

The first thing you need to do is create a google cloud account, create a project, and activate cloud functions in the gcloud console

Next step download the SDK

Now what?

Now, for now (05/2019) you can choose wheter to work in GOPATH, or use gomodules

Here it’ll be explained using gomodules.

Gomodules?

Gomodules are useful to pack dependencies in a “easy” way

Ok i’m ready

Great! let’s take a look at the function we’ll implement, this example won’t just be an helloworld, we’ll use dependencies, and communicate with google firestore

What is firestore?

Firestore is a really cool database available on gcloud, it is not hard to use, but we’ll talk deeper about another time

Ok let’s see the code

package pack

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	"cloud.google.com/go/firestore"
)

func Retrive(w http.ResponseWriter, r *http.Request) {
	ctx := context.Background()
	client, err := firestore.NewClient(ctx, "PRJ ID")
	if err != nil {
		fmt.Println(err)
	}
	defer client.Close()
	dsnap, err := client.Collection("expData").Doc("data").Get(ctx)
	if err != nil {
		fmt.Println(err)
	}
	dataIn := dsnap.Data()
	b, err := json.Marshal(dataIn)
	if err != nil {
		return
	}
	fmt.Fprintf(w, string(b))
}

There three important parts here: the import of firestore, or any other package, the function parameters, and the Fprintf at the end, also remember to put the project ID in the firestore client initialization, and to activate firestore too!

Then in firestore create a collection called “expData” and a document called “data” with some data in it

The firestore package is the reason we need gomodules, let’s see how to set it up

Where do I copy the function?

I like to work in the gcloud directory, I open the gcloud console and type “explorer .” (windows) to open an explorer tab in the folder

Ok, I opened the gcloud directory, created a new folder, and copied the function in a file named function.go now what?

now open a new cmd, in the function’s folder and run the following commands

go mod init ~Pack Name~
go mod tidy

I substitue ~Pack Name~ with pack wich is the package of the function?

Yes, exactly, now you are ready to deploy the function

Open the gcloud command line, and set the current project

gcloud projects list

will show all available projects

gcloud config set project ~Project Name~

remember to substitute~Project Name~ with the project name, this command will set the current project to deploy to

Is it time to deploy already?

Oh yess!

gcloud functions deploy ~Func Name~ --runtime go111 --trigger-http

This command will deploy thr function ~Func Name~, in our example “Retrive”, and will assign a trigger to it to be called with http, there are a lot of different triggers to look at not just http

In this case the content if the document will be sent to the requester in the string representation of a json

Good to know! thanks!

One thought on “How to use Google’s cloud functions (GoLang)

  1. Aw, this was an incredibly good post. Finding the time and actual
    effort to create a good article… but what can I say… I hesitate
    a lot and don’t seem to get anything done.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.