Member-only story

Stop Mixing Constants With the Type String in Golang

Golang’s untyped constants might make your work easier. But there’s a catch

Alexandre Couëdelo
Better Programming
3 min readMay 13, 2021

--

a rack of test tubes, one of which is filled with a green liquid
Photo by Bill Oxford on Unsplash

If there is one thing that irritates me when I use a library, whatever the programming language, it’s when the author leaves me clueless about what argument I should provide to a function. It gets worse when the function expects me to provide a specific constant.

Go is a strongly statically typed language, and therefore I would expect to see some clues as to which constants fit into which function without my having to reverse engineer the library. My IDE should tell me what option I have to feed inputs to the function.

The core of this issue comes from code like this:

Try it in the playground: https://play.golang.org/p/EMEvOL-zSVA

The two untyped string constants (FormalGreeting and CasualGreeting) don’t provide any information as to where to use them. Conversely, who would guess without looking at the source code that constants exist as intended inputs for the…

--

--

Alexandre Couëdelo
Alexandre Couëdelo

Written by Alexandre Couëdelo

Software Supply Chain and Automation Specialist (aka. DevOps).

No responses yet

Write a response