DEV Community

Evin
Evin

Posted on

A bug caused by silent defaults

I want to tell a story about how a seemingly innocent and often useful language feature caused us an embarrassing bug in production.

The feature is Go's behavior of allowing fields to be omitted in struct literals and having their type's zero value as a default. This feature has definitely been convenient for me before, but today it caused me such an embarrassment that I almost wish the language didn't have it.

I added a new field to a domain model in our app. However, I forgot to copy the value over in some of the places where a new instance of this struct is created from an old one, causing it to be set to 0. This resulted in us showing incorrect prices on our project costs report. Also, this was the third bug this week that this customer has encountered.

:((((

Discussion (4)

Collapse
freedom profile image
Freedom

Like this?

medium.com/swlh/object-oriented-pr...

Perhaps, it could useful for golint-ci to catch this silent bug.

Collapse
yujiri8 profile image
Evin Author

I don't know how golint-ci could've caught it without warning on every usage of this feature. There are some times where you do want the field left on its zero value

Collapse
blessinghirwa profile image
Blessing Hirwa

This is the interesting thing about programming lol.

Collapse
codemonkey51 profile image
Codemonkey51

Interesting bug, good luck with the product you are working on!