CyberSpy

Rantings from a guy with way too much free time

Channel Your Inner Gopher

2017-12-13 Programming Rob Baruch

Channeling your Inner Gopher - (Literally) Reflecting upon Channels

Many gophers are likely familiar with the communication paradigm, channels. An elegant solution to communicate (uni or bidirectionally) typed information among go-routines. In it’s simplest form, we declare as type-valued channel variable, make it, and then send and receive data through it. Easy enough!

package main

import (
        "fmt"
)

func main() {
        var simpleChan chan int = make(chan int)

        go func(c chan int) {
                // send important data to the channel
                c <- 42
                close(c)
        }(simpleChan)

        // receive data
        num := <-simpleChan
        fmt.Printf("got %d\n", num)

}

In our trivial example above, we create an chan int typed-channel and initialize it using make.

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
The soundcloud player can not be loaded with disabled JavaScript.
The following title is embedded here:
https://soundcloud.com/user-338167339/sabreras-etude-11-book-2

Sagreras Book 2, Lesson 11

2017-11-28 Music Etudes Rob Baruch

Etude Sagreras Lesson 11

Here’s my first cut at recording Sagreras Etude #11. Although the etude feels like it’s easy to play, recording it reveals an altogether unsatisfying experience when played back to the ear.

My goal is to show how the exercise evolves over time when practicing it with the intention of not just playing it as a site-reading piece, but rather practicing it with the intention of recording it with more attention beyond mere site-reading. Wish me luck!

Continue reading
Older posts Newer posts