Custom JSON Marshaling in Go

Go’s standard library has a convenient way of handling JSON verification and default values through the Marshaler and Unmarshaler interfaces. This means we don’t necessarily need separate methods to handle this data verification/manipulation. Unmarshaler Let’s pretend our system can’t allow users that are under the age 13 and we need a default timezone. var ( ErrTooYoung = fmt.Errorf("too young") ErrTZNotFound = fmt.Errorf("invalid timezone, must be one of %v", timezones) ) type TimeZone string var timezones = [....

February 19, 2023 · 4 min · 717 words · John Hooks

Custom Http Handlers Part 2

In the last post I covered a way to pass data to http handlers without using context.WithValue(). I saw another interesting way to do a type of dependency injection in a package from Jeremy called Mixer. If you own the server implementation and don’t want to import a 3rd party package you can do something similar. This doesn’t rely on generics so it is backwards compatible with older versions of Go....

October 8, 2022 · 6 min · 1108 words · John Hooks

Custom Http Handlers and context.WithValue()

One thing I try to avoid when I can is using the context.WithValue(). It’s a black box map of map[interface{}]interface{}. This is obviously flexible because anything can be stored here but there are pitfalls. The documentation states you need to create your own types for keys: The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context....

September 28, 2022 · 6 min · 1119 words · John Hooks

Piggy Bank

Overview Piggy Bank was born from a desire to easily store accessible secrets anywhere and not needing to expose HTTP ports for things like Vault. This project is no where near as robust as Vault (hence the name Piggy Bank) but it is desirable to be able to store secrets in the NATS KV store and have secrets be accessible to services using the bus. Authentication Authentication and authorization is done by using normal NATS auth....

August 29, 2022 · 3 min · 431 words · John Hooks

Goless

Overview It’s been a goal of mine for a while to write a Kubernetes operator. I was playing with Kubeless and I didn’t like how they implemented their support for Golang so I decided to write my own. Operators Operators are a deployment of a Kubernetes controller and Custom Resource Definitions (CRDs). The controller watches in a loop for new custom resources created and then takes some action. The controller lets you extend the ability of Kubernetes without directly changing any Kubernetes code....

October 2, 2021 · 2 min · 389 words · John Hooks

Functional Options

I was looking at ways to do method chaining in Go and found this article from Jon Calhoun about functional options. He made some really good points. I read through it probably three times, and being the dull knife that I am had trouble wrapping my head around it. So I sat down and tried to figure it out. I figured I’d put this post together for anyone who is in the same boat as me and wants a nice simple example of how functional options work....

August 4, 2020 · 4 min · 684 words · John Hooks

Goniq

In my journey to better my development skills I’ve been trying to write some small Go applications. I needed a sorter like uniq but on a Windows machine for a project I was doing. So I decided to write one. Unlike uniq the input does not need sorted beforehand. The program takes either stdin or can read from a file which is passed as the first argument. stdin: echo "test thing test thing other" | goniq...

May 25, 2019 · 1 min · 148 words · John Hooks