Golang

Generate All Boolean Array Combinations

While writing Go tests for goxplorer, I wanted to test all boolean flags combinations without having to cascade for loops.
This first method came to mind:

package main

import (
	"fmt"
)

func stob(bf []bool, s string) {
	for i, b := range s {
		if b == '0' {
			bf[i] = false
		} else {
			bf[i] = true
		}
	}
}

func main() {
	bf := []bool{false, false, false, false, false}

	for i := 0; i < 32; i++ {
		b := fmt.Sprintf("%b", i)
		stob(bf, b)
		fmt.Println(bf)
	}
}

Because the fastest way of generating every possible combination of 5 bits is to count from 0 to 31, i.e. 2^5, i.e. 32. The second trick here is to use fmt.Sprintf("%b") to generate a string representing the binary value.
It works, but I found the idea of using strings too heavy for a “binary” task.
A friend came to me with a barbaric bits field solution I found way too smart / complicated :), so I thought about another option but also using bits field, and thought about the following: how to tell if a field is a 1? One of the many options is the or binary operator; indeed, if a field is 0 and is “or’ed” with a 1, its value will become 1, if the initial value is 1, it will not change. Remember or truth table?

Is LevelDB 2 times faster than BadgerDB? (Update: No)

Update (2020/05/21) the method used in this post is totally sub-performant, and I finally found out about LevelDB’s and Badger’s batch methods, which make writes considerably faster, I’ll probably write another note about this.
And by the way, I found Badger to be much faster at writing batches than LevelDB.

Actual post

I’m working on a plugin for Goxplorer that will create a database of all Bitcoin addresses present in its blockchain.

Gitlab CI caching for Go projects

The reference documentation when it comes to couple golang and continuous integration in Gitlab is this one, it’s well put, easy to read and pretty accurate. Except for the caching part, or at least nowadays with go modules. This is what happens when a commit is pushed with the .gitlab-ci.yml given as an example in that document:

131 Creating cache default...
132 WARNING: /apt-cache: no matching files
133 WARNING: /go/src/github.com: no matching files
134 WARNING: /go/src/gitlab.com: no matching files
135 WARNING: /go/src/golang.org: no matching files
136 WARNING: /go/src/google.golang.org: no matching files
137 WARNING: /go/src/gopkg.in: no matching files

And as a matter of fact, the cache is empty for next stage.

Understanding golang channel range... again

In a previous article, I tried to explain my understanding of go channels interaction with ranges. Turns out my explanation was probably not clear enough because here I am, nearly a year after, struggling to achieve pretty much the same exercise.

So here we go again, on a good old trial and error fashion progress.

The goal here is to retrieve channel messages that are pushed from go routines created in a for loop.

proof-of-work based blockchain explained with golang

Yet another “blockchain explained” article, I know, I really thought about if releasing it or not, but you know, you only understand what you can explain clearly, so I hope I’ll be able to explain proof of work and blockchain as clearly as it is clear in my mind.
The originality of this post is that I’ll try to make those concepts clear through pieces of code extensively explained so it doesn’t feel like a theoretical expose where you get the idea without the taste.

golang reflection tips

Because I’m the kind of person who likes genericity, I often find myself using features of languages that are flagged as “only use it if you know what you’re doing”. Golang reflection is one of those features, powerful yet a bit confusing.

Reflection, as explained in The Laws of Reflection is the ability of a program to examine its own structure, particularly through types; it’s a form of metaprogramming

In short, you can introspect variables at run-time, making your program exceptionally dynamic. How can this serve any purpose? well imagine for example creating function names dynamically by another function parameter. Pretty cool uh?

Understanding golang channel range

Here we go again with my golang self teaching, today with a topic I had hard time understanding correctly (and hope I actually did): range over channels along with goroutines.

First of all, let’s have a little reminder. We all know a goroutine live its own life and must be waited for at the main level, i.e. in this example:

package main

import "fmt"

func main() {
	go func() {
		fmt.Println("hello there")
	}()
}

run me on playground

Golang interfaces, a pragmatic explanation for the programmer

I’m still in the process of learning golang the right way. Yes I already wrote some projects with the Go language (here and here), but I like to understand the real meaning of techniques when using them.
One of what is said to be the most amazing features of Go is interfaces. Here’s what the official golang docs has to say about it:

Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. We’ve seen a couple of simple examples already; custom printers can be implemented by a String method while Fprintf can generate output to anything with a Write method. Interfaces with only one or two methods are common in Go code, and are usually given a name derived from the method, such as io.Writer for something that implements Write.