Cryptolosophy     About     Articles     Papers     Subscribe

Harnessing LIFO Defer Stacking in Go

One of Go’s many useful features is the defer statement. It allows you to say, “I don’t want to do this right now, but please do it when this function returns”.

Consider the following code:

package main

import "fmt"

func main() {
	fmt.Println("start counting")

	for i := 0; i < 3; i++ {
		defer fmt.Println(i)

	fmt.Println("finished counting")

Deferred statements are executed in last-in-first-out order, so this will output:

start counting
finished counting

You can test it out for yourself on the playground.

But why is this property useful, and how can we use it to our advantage? Well, consider a slightly more complicated example:

// EqualBytes compares the contents of a container with a given slice.
func (b *container) EqualBytes(buf []byte) bool {
    // Attain the mutex.
    defer b.Unlock()

    // If sealed, first unseal.
    if b.sealed {
        defer b.reseal()

    // Compare and return the result.
    return bytes.Equal(b.buffer, buf)

Here we see a comparison method for some object, a *container. This object exposes a mutex and also has multiple states: sealed and unsealed. There’s two defer statements; the first releases the mutex and the second reseals the container.

The obvious problem (?) is that after the function returns, there may be a delay before the container is resealed and so another thread could mistakenly access or modify the unsealed container before we have a chance to reseal it.

Or could it? Look again. With LIFO, the order of execution will be:

// -> function returns


The way defer is designed made it so that we still own the mutex when we unseal, and so as long as we designed the rest of our API correctly, there should be no accidental access issues in multi-threaded applications.

It’s worth thinking about this useful property the next time you use defer in your code, and really taking advantage of it.

The Pursuit of Posthumous Peace

The origin of religion is likely in the human need for some way to cope with the unknown. Each person must formulate some internal philosophy for their existence which will then guide their outlook and actions in life. The philosophy that you decide to stick with will have a pretty significant impact on you, down to who you are as a person and how you think.

The most common is probably the heaven or hell ultimatum, aka infinite reward vs. infinite retribution. Followers of this hypothesis believe that they are in control of some probability of reaching heaven, where is the set of possible choices made in a lifetime. In this situation, the control over is what lends the believer comfort in death.

But it is a precarious comfort. See, the ideal behaviour of is such that as you approach death, the probability of achieving infinite reward should approach one. As in, as you age, you’ll control the in such that,

As long as this trend exists, you may lead a happy life (or at least a life without worrying about what will happen when you die). Of course at could also suddenly be close to, without converging to, one (and vice versa), but that hardly constitutes a life free of worry.

But if this trend doesn’t exist—say the opposite is true and actually approaches zero—then what? Your problems have been exacerbated and now instead of worrying about the unknown, you’re worrying about eternal damnation.

(There’s also the consideration that having to be careful about all is a pretty restricting way to live, but if one can find happiness in it, as many do, then that’s not an issue.)

The opposing view is that there is no god and that after death there is absolutely nothing. You cease to exist, and there is no longer a you to say “I am”. The question you could ask yourself is why—if we didn’t experience the first ~14 billion years before we existed—would we experience the next N after we stop existing?


xkcd: Beliefs

This view stems from various different schools of thought (simulation hypothesis, atheism, agnosticism, etc) that all share the conviction that we are simply the inevitable result of probability, time, and the principle of natural selection—since that’s the scientifically accepted model of the universe. The idea is to believe only what you can justify believing and discard everything else.

It is a comforting belief to hold too: when someone you love inevitably shuffles off this mortal coil, you will know that they’re not suffering. Everyone is guaranteed to be at ultimate peace, which is a gift that monotheism cannot provide. By adopting this philosophy you will lose out on the potential infinite happiness promised by monotheists, but you will also ultimately be avoiding hell.

There’s also a third option: reincarnation. It shares some resemblance with the monotheistic approach in that control over your actions, , determines your afterlife, but that’s where the similarities end. Instead of being designated to heaven or hell, you are instead “reborn” as a new being where you will again be given control over your actions, and again, and again, each time either improving or worsening the situation in your next life.

Reincarnation is an interesting one because it focuses on individual peace as opposed to comfort in relationships. You are constantly trying to live your own best life such that your next one is even better, all the while being unable to remember your past. It provides similar levels of comfort in death as monotheism does, and yet neither the happiness nor the suffering are eternal. There’s always another chance.

But at the end of the day, all of these “philosophies” are just different things that we tell ourselves to keep away the monster at night. Our beliefs are potentially limitless and our knowledge is limited by our senses, so what do we really know?

We just have to choose.