Ernest Micklei's Blog

things I create and problems I solve, to remember and share

Kimtato, a simple physics playground

Kimtato is the name of a simple interactive demo that runs on the Ubanita platform. It is a playground of physical objects which are created by players using touch gestures on the controller. Once created, the objects can be pushed around using the two-finger gesture, we call edge input mode. The following will discuss the making of this demo using the Ubanita Script API. get dimensions To position objects on the 2D scene, it can be useful to known the actual dimensions of that scene.

Testing your REST api in Go with forest

Go package forest was created to simplify the code needed to write real tests of a REST api. Such tests need to setup Http Requests, send them and inspect Http Responses. The Go standard library has all that is needed to achieve this flow but the required amount of code, especially the error handling, makes tests harder to read and longer to write. Testing github Let’s create a few tests that examine the response of the public github API call.

Artreyu, an artifact assembly tool

Artreyu is an open-source command line tool for software build pipelines that need to create artifacts which are composed of multiple versioned build parts. The design of this tool is inspired by the Apache Maven project which provides assembly support for Java projects. I created Artreyu to support a project that delivers compositions of many non-Java artifacts of which some are operating system dependent. Artreyu works with a repository to archive, fetch and assemble artifacts.

about

Who am i Software Artist @ bol.com

Line scanning in Go

Today, I needed to keep track of the linenumber while scanning table driven tests. The standard Go bufio.Scanner does not provide such information. Fortunately, in Go you can create your own by embedding the standard type and overriding the right function. That’s it. import ( "bufio" "io" ) type linescanner struct { *bufio.Scanner line int } func newScanner(reader io.Reader) *linescanner { return &linescanner{bufio.NewScanner(reader), 0} } func (l *linescanner) Scan() bool

Guava-like EventBus for Go

I needed a simple solution to notify components about changes in some global settings. In my Java days, I have been using the Guava Eventbus to solve similar problems, so I decided to cook something that does the basics. From Guava: “The EventBus allows publish-subscribe-style communication between components requiring the components to explicitly register with one another (and thus be aware of each other).” The Java implementation uses annotations to register components and subscribe its methods to event types.

Smalltalk collect on Go slice of int

package main import "fmt" type intSlice []int func (i intSlice) collect(block func(i int) int) intSlice { r := make(intSlice, len(i)) for j, v := range i { r[j] = block(v) } return r } func main() { numbers := intSlice{1, 2, 3} squared := func(i int) int { return i * i } fmt.Printf("%v", numbers.collect(squared)) } Try it. Implementations of select,inject,detect are left as an excercise for the reader.

Javascript CLI in Go using Otto

Today, I was playing with Otto and hacked together a small command-line-interface program in Go. package main import ( "bufio" "fmt" "os" "strings" "github.com/robertkrimen/otto" ) var Otto = otto.New() func main() { fmt.Println("otto\n") loop() } func dispatch(entry string) string { if len(entry) == 0 { return entry } value, err := Otto.Run(entry) if err != nil { return err.Error() } else { return fmt.Sprintf("%v", value) } } func loop() { for { fmt.Print("> ") in := bufio.NewReader(os.Stdin) entered, err := in.ReadString('\n') if err != nil { fmt.Println(err) break } entry := strings.TrimLeft(entered[:len(entered)-1], "\t ") // without tabs,spaces and newline output := dispatch(entry) if len(output) > 0 { fmt.Println(output) } } } One cool feature of Otto is the ability to expose your own Go functions in the Javascript namespace.

From multiple to single-value context in Go

These are the results of exploring one aspect of the Go language; functions can return multiple values. Consider the following simple function: func ab() (a,b int) { a,b = 1,2 return } Clearly, this function cannot be used in other function calls that expect a single value. fmt.Printf("%v\n",ab()) The compiler will help you: “multiple-value ab() in single-value context”. Now suppose you want to take only the first value without introducing an intermediate variable.

A case of sizing and draining buffered Go channels

Update: Based on several comments posted on the G+ Go community, I rewrote the solution presented here. See past the Conclusion section. Recently, I was asked to write a small program for collecting product promotion information for a large collection of products. Given an input file of product identifiers, the following tasks had to be done: read all identifiers from an input file for each identifier, fetch product offer information in XML parse the offer XML into in a DOM document extract all promotions from the offer document for each promotion, export its data into a CSV record A straightforward implementation is to write a function for each task and call them in a nested loop.