CyberSpy

Rantings from a guy with way too much free time

Getting func(y) with Channels

Here’s a little golang puzzle

Take a look at the function below that computes a mystery value for the function $f(x)$. Any idea what the function is? Take a close look at the first line where we create a channel. Notice that the channel isn’t some boring int or string channel. No, this is a channel of type func(int, chan int, chan bool). Wait what??? That’s right, this channel takes a function that takes an integer and two channels as function parameters. We define such a function, x, right aftewards.

Continue reading

Once Upon a Reflection: Looking Deeper into Golang reflection

I often reflect upon my code…

One of the coolest features of the Golang programming language is the reflect package. As the package documentation states at the onset of the package:

Package 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.

On the surface, that all sounds pretty straight-forward. But when you start to think about the types of objects (literally) that you might reflect upon, things can get interesting pretty quickly. It’s one thing to dynamically get the type of an object like an Int, and then extract it’s value. But what about dynamically getting access to Methods inside a struct and then calling it with values created dynamically from an a-priori signature, returning values and extracting those values programatically? Rather quickly, the power of reflection can become overwhelming and even confusion. Moreover, appropriate use-cases of reflection should be well thought out so that we aren’t simply getting too clever for our own good.

Continue reading

Wanna learn you some Go? Tutor up over here!

Rob

Go Get Interfaced: Enums in Golang

Golang, Interfaces, and Enums

So here’s a nice golang idiom that I ran across years ago that I found generally useful. Golang, unlike languages like c doesn’t natively support enumerations. Instead, constants typically are used when creating a list of enumerations. But, go is a strongly-typed language, so we can do better than simply using constants - we can type our enumeration with the use of a type declaration.

type DogState uint

By defining a type for our enumeration, we can consistently pass and return typed-values among our functions operating on our enumeration. Okay, that’s all well and good, but what happens when we want to go between uint values and string representations of those values; a commonly used paradigm when working with enumerations. One approach would have us write mapping functions that might switch among the values to return a string. Or, when going from a string name of an enumeration to the uint value, we might use a map[string]. That’s a doable implementation, but there’s an easier, and more idiomatic way.

Continue reading

Gogo

Here we gogo!

Years ago, I wrote an interesting article that I thought might be worth re-posting (and revising) here on my blog. For a while I got into programming in golang and in the early going (pun-alert!), there were a lot of idioms that were not well understood by a noob. One of those paradigms was channels, go-routines, and signals used simultaneously. Taken separately, they are more easily understood. But when taken together, there can be some confusion. In this article, I address how to properly establish a signal-handler and a channel to terminate go-routines when the user interrupts the program with a signal such as ctrl-c or kill(1).

Continue reading