explaingit

yywing/go-defaults

Analysis updated 2026-07-13 · repo last pushed 2019-09-17

GoAudience · developerComplexity · 2/5DormantSetup · easy

TLDR

A small Go library that automatically fills in default values for data fields using simple tags, so you don't have to write repetitive setup code for every new data structure.

Mindmap

mindmap
  root((repo))
    What it does
      Tags fields with defaults
      Fills empty values automatically
      Leaves tagged fields alone
    Tech stack
      Go
      Reflection
    Use cases
      Config settings
      Data models
      Request objects
      User preferences
    Tradeoffs
      Zero values seen as empty
      Apply defaults first
    Audience
      Go developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Set default values for configuration structs without writing repetitive setup code.

USE CASE 2

Default user preference toggles to 'on' when building apps that accept user settings.

USE CASE 3

Pre-fill standard starting values for request objects and data models automatically.

What is it built with?

Go

How does it compare?

yywing/go-defaultsaasheeshlikepanner/vasealexzielenski/controller-runtime
Stars0
LanguageGoGoGo
Last pushed2019-09-172022-04-20
MaintenanceDormantDormant
Setup difficultyeasymoderatehard
Complexity2/54/54/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

Just run go get to add the library to your Go project and call SetDefaults on your structs.

In plain English

Go-defaults is a small helper library for the Go programming language that lets developers specify default values for data fields using simple annotations. Instead of writing repetitive code to manually set default values every time you create a new data structure, you just tag a field with default:"true" or default:"33", and the library handles filling in those values for you automatically. At a high level, when you call the library's SetDefaults function on a data structure, it looks at each field to see if you left a "default" instruction on it. If it finds one, it applies that value to the field. For example, if you have a field meant to hold a time duration and tag it with a default of "1m," the library will automatically set it to one minute. If a field doesn't have a default tag, it just gets left alone. Developers working in Go would use this when they have configuration settings, data models, or request objects where many fields typically have standard starting values. For instance, if you are building an app that accepts user preferences, you might want certain toggles to default to "on" unless the user specifically turns them off. This tool saves a developer from writing boilerplate setup code over and over. There is one notable tradeoff to be aware of. The library decides whether to fill in a default by checking if a field is currently empty. In Go, "empty" means the zero value for that type, like 0 for numbers or false for toggles. This means if you intentionally set a number field to 0 before applying defaults, the library will see that 0 as "empty" and overwrite it with your specified default. To get around this, developers need to apply their defaults first, and only then manually set any fields that genuinely need to be zero.

Copy-paste prompts

Prompt 1
Help me use the go-defaults library in my Go project. I have a Config struct with fields like Timeout time.Duration, Retries int, and Verbose bool. Show me how to tag them with default:"1m", default:"3", and default:"true", then call SetDefaults.
Prompt 2
I'm using go-defaults and I have a struct where one number field needs to genuinely be 0. The library keeps overwriting my 0 with the default value. How do I work around this zero-value issue?
Prompt 3
Show me how to integrate go-defaults into an existing Go API handler where I parse JSON request bodies into structs, so that any fields the client didn't send get filled with sensible defaults.

Frequently asked questions

What is go-defaults?

A small Go library that automatically fills in default values for data fields using simple tags, so you don't have to write repetitive setup code for every new data structure.

What language is go-defaults written in?

Mainly Go. The stack also includes Go.

Is go-defaults actively maintained?

Dormant — no commits in 2+ years (last push 2019-09-17).

How hard is go-defaults to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is go-defaults for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.