This article provides users with a general guide to the features listed.
These features may have been updated or superseded by additions found in the release notes.
Read the content below to become familiar with the feature and review the release notes to get the latest iteration.
Overview
GoBQ mode allows users to run golang code in a sandboxed environment
Run GoBQ
To run a GoBQ script switch to Mode: GoBQ
|
1. GoBQ script can be written in the GoBQ text area a. Return value shows anything returned by the script |
GoBQ Script
func MainReturn
func mainReturn() interface{} { return "myvalue" } |
mainReturn is the GoBQ entry point. When Run Script is clicked function mainReturn is executed |
Data Types
GoBQ supports various data types that have an equivalent in Goliath (primarily for parameter usage)
Data types are especially important when assigning values to parameters
When assigning values from a script to a parameter sync the golang data type (script value) to the Goliath data type (Goliath parameter value is assigned to)
Supported golang data types | Respective Goliath data types |
bool | boolean |
string | string |
int, int8, int16, int32, int64 | number |
uint, uint8, uint16, uint32, uint64 | number |
float32, float64 | number |
Data Type Returned Value
Example 1 func mainReturn() int {
return 1 } Example 2
func mainReturn() string { |
The datatype of the returned value can be specified as shown in the examples on the left Example 1 shows returned value as datatype int
Example 2 shows returned value as datatype string
|
Data Type Parameters
|
Goliath parameters can also be used with GoBQ
Be sure to use the appropriate data type when using parameters
|
Supported packages
Import statements are not allowed in playground
Instead a number of packages are already imported and can be used in GoBQ
Imported packages include:
Package | Description |
reflect |
Implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type. A call to ValueOf returns a Value representing the run-time data. Zero takes a Type and returns a Value representing a zero value for that type. See "The Laws of Reflection" for an introduction to reflection in Go: https://golang.org/doc/articles/laws_of_reflection.html |
encoding/json |
Implements encoding and decoding of JSON as defined in RFC 4627. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions. See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html |
errors | Implements functions to manipulate errors |
time |
Provides functionality for measuring and displaying time. The calendrical calculations always assume a Gregorian calendar |
math | Provides basic constants and mathematical functions |
strconv | Implements conversions to and from string representations of basic data types |
strings |
Implements simple functions to manipulate UTF-8 encoded strings. For information about UTF-8 strings in Go, see https://blog.golang.org/strings |
regexp |
Implements regular expression search. The syntax of the regular expressions accepted is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by RE2 and described at https://golang.org/s/re2syntax, except for \C. For an overview of the syntax, run |
crypto/sha256 | Implements the SHA224 and SHA256 hash algorithms as defined in FIPS 180-4 |
crypto/hmac | Implements the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key |
cloud.google.com/go/bigquery | Provides access to the BigQuery API |
cloud.google.com/go/storage | Provides access to the Cloud Storage JSON API |
crypto/md5 | Implements the MD5 hash algorithm as defined in RFC 1321 |
bytes | Implements functions for the manipulation of byte slices. It is analogous to the facilities of the strings package |
sync |
Provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication. Values containing the types defined in this package should not be copied |
func mainReturn() int { var x, y int = 3, 4 var f float64 = math.Sqrt(float64(x*x + y*y)) var z uint = uint(f) xPrintln(x, y, z) return x } |
Supported packaged can be referenced as usual In this example the package math (math.Sqrt) is referenced |
I/O functions
GoBQ mode provides limited access to a set of fmt functions
These functions can be referenced by their alias
Functions and their alias are:
Supported fmt methods | Alias to use in GoBQ |
fmt.Sprint | xSprint |
fmt.Sprintf | xSprintf |
fmt.Sprintln | xSprintln |
fmt.Print | xPrint |
fmt.Printf | xPrintf |
fmt.Println | xPrintln |
fmt.Fprint | xFprint |
fmt.Fprintf | xFprintf |
fmt.Fprintln | xFprintln |
func mainReturn() int { var x, y int = 3, 4 var f float64 = math.Sqrt(float64(x*x + y*y)) var z uint = uint(f) xPrintln(x, y, z) return x } |
Supported I/O functions can be referenced via their alias In this example the I/O function fmt.Println is referenced via alias xPrintln |
BigQuery Package
Obtain access to BigQuery API (https://pkg.go.dev/cloud.google.com/go/bigquery)
- To obtain bigquery.Client type: func getBigqueryClient(billingProjectId)(*bigquery.Client, error)
- Above returns bigquery.Client with user credential and billing project initialized
- client, err := getBigqueryClient(billingProjectId)
- With above variable user has access to BigQuery API (https://pkg.go.dev/cloud.google.com/go/bigquery#Client)
Storage Package
Obtain access to Cloud Storage API (https://pkg.go.dev/cloud.google.com/go/storage)
- Obtain storage.Client type: func getStorageClient()(*storage.Client, error)
- Above returns storage.Client with user credential
- client, err := getStorageClient()
- With above variable user has access to Cloud Storage API (https://pkg.go.dev/cloud.google.com/go/storage#Client)
Examples
When adopting samples from BigQuery Samples to work in Goliath, the following adjustments are needed:
- Remove import statement as this is not supported in Goliath Go Editor.
- Create Bigquery client with user credential by
- Replacing:
client, err := bigquery.NewClient(ctx, projectID)
By:
client, err := getBigqueryClient(projectID) - Place code to be executed in the following entry point function
func mainReturn() interface{} {
//code to be executed
}