Sneaky Defers In Go

What do you think the output of the following code would be? package main import "fmt" func main() { input := "hello" TestDefer(&input) } func TestDefer(input *string) { defer fmt.Println(*input) *input = "world" fmt.Println(*input) } Given how defer-ed functions are executed just before the parent function exits, I expected the output to be world world But, on execution it actually prints world hello This is because the arguments are evaluated when the defer is encountered, and not when the deferred function is actually called. Effective Go even has a line specifically about this behavior (which I discovered later). ...

2021-07-06 · Athul Suresh

Writer's Block

A while back, I came across this article by Josh Branchaud where he talked about TIL posts and learning in public (among many other things). This really appealed to me. Over the years, I had accumulated immense amount of information from posts and articles that people had put out and the whole idea of paying it forward by putting out things that I’d learned along the way sounded interesting. The primary impediment I faced here was the amount of time it took to create a well-crafted post. Most of the longer pieces here were summaries of months of effort squeezed into a single page post with pictures. Those were few and far apart. ...

2021-07-04 · Athul Suresh

Book Review: Normal People

Do I hate this book? Yes. Why do I hate this book? Like the confused protagonists, I don’t have a clear answer for that. Let me get the good part out of the way, so that I can thoroughly whine about just how much this book irritated me. I’m a sucker for books that focus on and obsess over the inner workings of the protagonists. There is no grand adventure or suspense here. It is the inner monologue and emotional turmoil of two characters that drive the plot, which I absolutely love. There is a certain degree of introspection that took place whenever I saw a part of myself streaked across early Connell’s mannerisms or in how he viewed the motivations or repercussions of his actions. ...

2021-07-02 · Athul Suresh

Back After A Hiatus

I realized that I hadn’t touched my blog in over a year. A lot had happened since then, but I hadn’t taken the time to note anything down. Looking through my old posts, I remember how good it felt to actually sit and write things down. I thought I’d change the theme and tweak things a bit just for the sake of it. Lo and behold, I ended up spending an entire day trying out themes and tweaking knobs and controls to see how things looked. Sometimes, I feel like I waste a lot of time on preliminary aspects of a task. ...

2021-06-03 · Athul Suresh

Un-implementable Interfaces In Go

Recently, I started randomly going through the Go standard library, mostly to satiate my curiosity and to find out what goes on behind the curtains. While checking out the testing package, I found this interesting little snippet of code in src/testing/testing.go: // TB is the interface common to T and B. type TB interface { Cleanup(func()) Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Failed() bool Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Helper() Log(args ...interface{}) Logf(format string, args ...interface{}) Name() string Skip(args ...interface{}) SkipNow() Skipf(format string, args ...interface{}) Skipped() bool TempDir() string // A private method to prevent users implementing the // interface and so future additions to it will not // violate Go 1 compatibility. private() } This seems pretty evident once you see it. It makes sense for the Go standard library where the private function enables them to circumvent the compatibility promise by ensuring that no one would be able to use this interface outside of the standard library because of the private function. This gives them the flexibility to add functionality later without breaking anything. ...

2021-05-07 · Athul Suresh