Thursday, June 12, 2014

Golang tip to check if a variable is an array: get the variable value with the reflect package and then the kind of the value: see http://play.golang.org/p/eUS6I4a50e

All possible kinds: http://golang.org/pkg/reflect/#Kind

If you know something simpler, let me know...

Code:

package main

import ("fmt"
"reflect")

func isArray(a interface{}) bool {
var v reflect.Value
v = reflect.ValueOf(a)
fmt.Println("a value: %T %v", v, v)

var k reflect.Kind
k = v.Kind()
fmt.Println("a kind: %T %v", k, k)

if (k == reflect.Array) {
return true
}
return false
}

func main() {
a := [...]string{"1","2"}
//a := "fred"
if (isArray(a)) {
fmt.Println("a is an array")
} else {
fmt.Println("a is NOT an array")
}

t := reflect.TypeOf(a)
fmt.Println("a type: %T %v", t, t)

var v reflect.Value
v = reflect.ValueOf(a)
fmt.Println("a value: %T %v", v, v)

var k reflect.Kind
k = v.Kind()
fmt.Println("a kind: %T %v", k, k)

}

Run:

a value: %T %v <[2]string Value> <[2]string Value>
a kind: %T %v array array
a is an array
a type: %T %v [2]string [2]string
a value: %T %v <[2]string Value> <[2]string Value>
a kind: %T %v array array

Tuesday, May 20, 2014

BigQuery Google App Engine (GAE) Java Example


I recently wrote a technical "How-to" on "Using the BigQuery API with Java in an Eclipse Project on the Google App Engine". The application uses a service account for authentication that allows an application to run without asking the user for specific credentials. It shows how to authenticate and make some API calls to BigQuery.

Being a servlet, the code is probably portable to another servlet container like Tomcat.

The project and code are here:


Happy BigQuerying!