How to test Go functions that make syscalls?

I’m not going to waste your time You can find a similar explanation at this github repo from schollii. The idea is simple, yet it might be tedious. You need to define an interface, and use that interface instead of calling os.Getenv, exec.LookPath and any other syscall making function you’re using. Then, you implement this interface with actually calling these function. For testing, you implement the interface with some mock you create or generate....

February 27, 2024 · 4 min · Dror Nir

Factor3 (v0) - a code gen for configuring your Go app

Photo by Peter Herrmann on Unsplash I wrote a small toy for loading configuration easily from multiple sources. I was thinking a lot about it, even wrote a whole post. Here’s a link to it The Problem I wanted a 100% opinionated library that would let me define a Go struct with the expected values and let it generate code that loads configuration from multiple sources, simultaneously: The default configuration will be defined in a yaml file The values in the config file can be overridden by environment variables, and those environment variables can be overridden by passing flags in the commandline....

January 8, 2024 · 3 min · Dror Nir

I rewrote a PHP script in Go

Photo by Ben Griffiths on Unsplash This is a short story detailing one of my successful projects, which I hope I’ll never need to repeat. I happened in the older times, in the pre-COVID era. SEO had an old PHP script The script was in fact a small PHP server running on a Windows server. It served a single unattractive HTML form allowing users to upload a SVC file and receive another CSV file in response....

January 7, 2024 · 4 min · Dror Nir

I Don't Get Why You Would Use WASM outside of a Browser

Photo by Techivation on Unsplash I don’t get why people want to use WASM if they don’t plan to run it in a browser. I mean, I do get it, I’m just not sure it’s the right choice. As far as I understand, people use WASM as a new standard for writing plugins for tools. WASM is yet another scripting language Historically, if you wanted to create a pluggable system, you would choose a high level language....

January 4, 2024 · 3 min · Dror Nir

`gonew` - the official Golang scaffolder is in alpha

Russ Cox, from the maintainers of Go, released a new prototype for the a new standard Go tool: gonew From the blog post, you can see it doesn’t do much, which feels very at home for Go: $ go install golang.org/x/tools/cmd/gonew@latest And then you can point to any go project on the web, and basically rename it: $ gonew golang.org/spf13/cobra example.com/my-cli $ cd ./my-cli Obviously, cobra is not a good candidate to use as a template....

July 31, 2023 · 2 min · Dror Nir

Why Code Generation?

Photo by Iñaki del Olmo on Unsplash Code Generation is a form of Metaprogramming From the early days of the C programming language, to today’s cutting edge languages, some sort of meta programming technique is used. By “meta programming”, I mean any form of programming that adds functionality to the code you write, without actually writing that extra functionality. According to Wikipedia: Metaprogramming […] means a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running....

July 31, 2023 · 8 min · Dror Nir

A Declarative Config for Golang

A Declarative Configuration lib for Go I’m thinking about building a Go package (and CLI) to let me just declare a schema of a config, and use code generation on top of that to declare how I want to Marshal and Unmarshal it into anything that makes sense. I want to try to make a case in favor of writing it before I actually roll up my sleeves and write it....

July 25, 2023 · 9 min · Dror Nir