RSSAmplifier

Blog

Office Hours

Go focused best practice column

titpetric.comRSS feed ↗10 posts

Latest posts

Implementation contracts in Go

For a long time, I considered a filled interface type as an implementation contract in Go. A very quick shorthand on how you can check for implementation compliance is to add the following snippet to your _test.go files: var _ rpc . UserService = & UserService {} Until your UserService meets the interface contract, the test will result in failure. You’re likely to add this check to ensure…

Modern (Go) application design

When it comes to application design, I’ve formed a few opinions backed by experience. The most important one is: structure matters . In my first years of development, I’ve built a CMS system that was copied over more than 100 times for different web pages. You don’t get there unless you repeat the same process over and over. Application development is like that. If you’re…

Service-Oriented Architecture with Docker and Caddy

Learn how to structure services using a clean service-oriented architecture powered by Docker and Caddy. The guide explores reverse proxy setup, handling of multi-container deployments, and scalable approaches to networking and credential management.

LSC - Large scale code changes

Let’s take a look at semgrep for semantic code matching and automated large scale changes.

Modify after write

A “modify after write” error refers to a situation where a program or process attempts to modify data after it has been written or committed to a particular location or state. In Go terms, this can mean that a field may be modified after the struct has been written to the database. When debugging a login issue, we found a field was not updated in the database. On investigation, I found…

Real world optimisation

Don’t optimize prematurely - but have a plan how you’re going to do it. With the services I write, a lot of the code may be clear, simple and readable over actually optimizing for speed. Sometimes, the difference between that code, and the optimized code, is a short refactor.

How we use logging

When it comes to logging in your Go services, the immediate package to reach for is the standard library log package . As it happens, you’ll outgrow the log package needs almost immediately, because the log package is able to send the relevant logs only to one io.Writer stream, by default sent to os.Stderr.

Go and JSON encoding/decoding

After a few years of writing JSON APIs for various purposes, with more success than this article may lead you to believe, I have come to the conclusion that the JSON support in the wider Go ecosystem is broken. And it isn’t really Go’s fault, but fixing it so it plays nice with other very common programming languages is something that hasn’t been given much consideration to.

Waiting on Goroutines

Go is a program language which has basic concurrency syntax built in. Maybe calling it basic isn’t exactly right - simplicity is the more correct description. After all to run a function independent of the main execution, all you have to do is prefix the invocation with the go keyword - that function call will now live on it’s own goroutine.

Extending pflag with environment variables

Chances are that if you’re writing Go code for a while now, your needs for command line arguments have grown beyond just what the standard library flag package is able to provide.