Go
Friday, June 7, 2024
Spent around two hours trying to diagnose why one of our containers couldn’t read secrets from AWS Secrets Manager. I was seeing errors of the following form when I was trying to call GetSecretValue:
operation error Secrets Manager: GetSecretValue, get identity: get credentials: failed to refresh cached credentials, not found, Signing Turned out to be a bug in version v1.29.2 of the secrets manager client: github.com/aws/aws-sdk-go-v2/service/secretsmanager. Downgrading the client version to v1.
Continue reading →
Wednesday, June 5, 2024
Go has had parallel tests enabled by default for a while now. But if you can’t run your tests in parallel, what should you do?
Most people probably know of the -p 1 option to disable parallel execution of tests:
go test -p 1 . But this only works if you have access to the command itself. If you’ve got the go test command in a Makefile running in a CI/CD environment that’s difficult to change, this may not be available to you (it wasn’t available to me when I faced this problem yesterday).
Continue reading →
Thursday, May 30, 2024
Subtitle: if you know what these four words mean, then this post is for you
This is a quick one as I’m not really in a blogging mood. But I couldn’t for the life of me find a way to decode a PostgreSQL bytea value (which is what PostgreSQL uses for BLOB values) using pgx and sqlc so that it would match what I actually stored.
The documentation of pgx and sqlc, along with various web searches, yielded nothing.
Continue reading →
Saturday, April 27, 2024
One of the craziest ideas I had recently was to move all my code from Github to a self-hosted SCM system. The impetus for this was to have import paths with a custom domain name for all my Go packages, rather than have all them start with github.com/lmika/(something). Fortunately, this proved to be unnecessary, as Go does allow one to customise the import path of packages hosted elsewhere.
This area of the docs has all the details, but here’s the process in short.
Continue reading →
Monday, April 15, 2024
You’re working on a Go project (yay! 🎉) and you need to write a unit test. You decide to go with a table-driven approach. What names should you use for your variables?
For a long while, I was writing tests using names like these:
func TestSomething(t *testing.T) { scenarios := []struct { description string arg string expected string }{ { description: "Thing 1", arg: "this", expected: "that" } // more cases go here } for _, scenario := range scenarios { t.
Continue reading →
Saturday, November 11, 2023
Here are some random notes about working with Buffalo (may it rest in peace).
Cleaning Up Assets In Buffalo Buffalo doesn’t clean up old versions of bundled JavaScript files. This means that the public/asset directory can grow to gigabytes in size, eventually reaching the point where Go will simply refuse to embed that much data.
The tell-tail sign is this error message when you try to run the application:
too much data in section SDWARFSECT (over 2e+09 bytes) If you see that, deleting public/assets should solve your problem.
Continue reading →
Sunday, November 5, 2023
Some random notes about the Go language.
Commas in Expression Case Statements The use of comma in case statements work for expression:
x := 2 switch { case x == 1, x == 2: fmt.Println("x < 3") default: fmt.Println("x >= 3") } If x is 1 or 2, the x < 3 will be printed out.
Lookups On A Nil Map You can do lookups on a nil map:
var xs map[string]string = nil x, hasX := xs["hello"] The result will simply be the zero value of the particular type.
Continue reading →
Wednesday, November 1, 2023
I spent the better half of last week trying to build a MacOS .app bundle for one of my Go projects. I want to eventually put this online for others to download, so it was time to work out how to go from a simple binary to a .app bundle.
I want something that is repeatable, automated, and could be done within a GitHub Action, and because this was a Go project, doing this from Xcode was out of the question.
Continue reading →
Saturday, October 28, 2023
The errors.New() function in emperror.dev/errors package includes context information, like stack traces. This means that if you were to use this for sentinel error types, i.e. error types like EOL, you’ll be including unnecessary stack information. This is especially useless when these are defined as global variables:
var ErrThing = errors.New("thing happened, but that's OK") Instead, use the errors.Sentinal type.
var ErrThing = errors.Sentinal("thing happened, but that's OK") Dave Cheney discusses why this works.
Continue reading →
Saturday, October 28, 2023
Go allows the injection of build-time variables, such as version numbers or Git hashes, directly into the code using the linker option -X
.
Continue reading →
Wednesday, October 25, 2023
Using go:generate to run tools over your Go application is super useful, but how can you know you’ve got the tool installed? Simply having something like:
//go:generate mockery --name=Provider --case=snake --with-expecter would only work if you’ve already got mockery installed.
If the tool is a Go application in a public repository, not a problem. Simply run go install and it’s now on your dev machine:
$ go install github.com/vektra/mockery/v2@latest $ go generate .
Continue reading →
Wednesday, June 23, 2021
Parametrising Cucumber tests using Go and Go’s templating language.
Continue reading →
Wednesday, December 23, 2020
A technique for using helper functions that can access the private symbols of a package, yet are only available to tests.
Continue reading →
Saturday, December 12, 2020
How to load and launch a Go WASM app in the browser.
Continue reading →