Go Template
This enables allows you to code functions using Golang 1.19. The
handle
function defines the HTTP request handler logic. This
function must implement the logic that the function should execute each time it is
invoked.package function
import (
"io/ioutil"
"net/http"
)
// Handle defines the logic of the HTTP request handler.
//
// This example echoes the HTTP request payload by returning a response with
// status code 200 and the same payload used in the request.
func Handle(w http.ResponseWriter, r *http.Request) {
var body []byte
if r.Body != nil {
defer r.Body.Close()
body, _ = ioutil.ReadAll(r.Body)
}
w.WriteHeader(http.StatusOK)
w.Write(body)
}
The
Handle
function is the HTTP Handler in which your code will run. You can use other functions in your
code to make it cleaner, but the Handle
function is the entry point
of your serverless function. Check these sources for further information about Go
handlers:Libraries
Adding third-party dependencies is not allowed at the moment. For now, you can use
the following pre-installed dependencies (this list will grow):
- resty: A simple HTTP and REST client library for Go.
- eclipse/paho.mqtt.golang: The Eclipse Paho MQTT Go client.
- gopkg.in/yaml.v3: Allows you to encode and decode YAML values.
SWX Package
The function/swx package allows you to obtain and revoke access tokens for the Altair
IoT Studio REST API.
Note: This package is in a very early
stage of development. More features will be added in the future.
Variables
You can access any variable information stored under the Variables Storage using the
two built-in functions in the function/variables
package:
func Get(variableName string) string
: Returns the value of the given variable. If the variable is not found, an empty string is returned.func Exists(variableName string) bool
: Returns a boolean value indicating if the given variable is found.
Example:
package function
import (
"function/variables"
"net/http"
)
func Handle(w http.ResponseWriter, r *http.Request) {
// ...
if variables.Exists("my_password") {
mySecret := variables.Get("my_password")
// ...
}
// ...
}
Logging
Anything you write to
stderr
is saved as a log entry. You can use
the log
package for that:package function
import (
"log"
"net/http"
)
func Handle(w http.ResponseWriter, r *http.Request) {
// ...
log.Println("My log entry!")
// ...
}
Examples
Here are some simple examples to illustrate how you can write your own functions using Go.
- Adder
- This is a really simple function that gets a comma-separated list of
numbers in the request body and return the sum of them (or an error if
the input format is invalid).
package function import ( "fmt" "io/ioutil" "net/http" "strconv" "strings" ) func Handle(w http.ResponseWriter, r *http.Request) { var body []byte var sum float64 = 0 //Gets function input if r.Body != nil { defer r.Body.Close() body, _=ioutil.ReadAll(r.Body) } // Parses input as a comma-separated list of numbers numbers := strings.Split(string(body), ",") //Tries to convert all values to numbers for _, number :=range numbers { number = strings.TrimSpace(number) if value, err :=strconv.ParserFloat(number, 32); err != nil { //Throws an error if something is not a number w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Invalid input format!")) return } else { sum += value } } w.WriterHeader(http.StatusOK) w.Write([]byte(fmt.Sprintf("%f", sum))) }
- MQTT Publisher
- The following example shows how to set up an MQTT client to publish the
payload of the request to the
functions/go
topic. - Update a Thing's Property
- The following example shows how to update a Thing's Property using the
swx
package to deal with token request and revocation.