Quick Go Dump
4 May 2022I am working on porting a Go SDK to Rust. My goal right now is to figure out the I/Os of a certain SDK function and replicate it in go.
I’m an lucky the previous maintainers provided an in memory client, which I should be able to override and json dump all the I/Os, which should become the input for my rust SDK tests.
Example
This is the commit where I test this approach:
https://github.com/laurentsenta/sdk-go/commit/cf5506c708a66f3f5e0579ab3e32cac60e858b54
It’s a [[TestGround]] hack.
Anonymous Structs
Define the struct and create it at the same time. It’s the fastest way to hack around and get a readable JSON output in cases where you want some data and it’s not straightforward to start a go debugger.
(see below why)
	i.operations = append(i.operations, Operation{Kind: "barrier", Payload: struct {
		State  State `json:"state"`
		Target int   `json:"target"`
	}{
		State:  state,
		Target: target,
	}})
Build in docker
docker run -t -i --rm --volume $PWD:/pwd golang:1.17-buster bash
That’ll mount the current folder and let you build from a docker / linux / buster image. Useful when you work on a piece of go code that doesn’t build on macos.
JSON Marshal indent
func (i *inmemClient) DumpIO() error {
	bytes, err := json.MarshalIndent(i.operations, "", "  ")
	if err != nil {
		return err
	}
	println(string(bytes))
	return nil
}

Laurent Senta
I wrote software for large distributed systems, web applications, and even robots.
These days I focus on making developers, creators, and humans more productive through IPDX.co.