explaingit

coreybutler/go-struct-defaults

Analysis updated 2026-08-04 · repo last pushed 2019-06-21

1Audience · developerComplexity · 2/5DormantSetup · easy

TLDR

A Go library that lets you tag struct fields with default values and automatically fills them in with a single function call, skipping any fields you've already set.

Mindmap

mindmap
  root((repo))
    What it does
      Tags fields with defaults
      Fills empty fields automatically
      Skips preset values
    Supported types
      Text and numbers
      Lists and maps
      Nested objects
    Use cases
      Config structs
      Predictable starting states
      Reduce boilerplate
    Advanced features
      JSON snippet defaults
      Opt-out tags
      Dynamic runtime defaults
    Audience
      Go developers
      App builders

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 sensible defaults on a large configuration struct with one function call instead of manually initializing every field.

USE CASE 2

Tag data model fields with default values so new objects always start with predictable, meaningful data.

USE CASE 3

Use runtime-computed values like timestamps or random numbers as defaults through a custom interface.

USE CASE 4

Leave specific fields untouched by defaults using an opt-out tag.

What is it built with?

Go

How does it compare?

coreybutler/go-struct-defaults0xallam/posthog0xallam/search-engine
Stars111
LanguagePythonC++
Last pushed2019-06-212026-03-262023-08-23
MaintenanceDormantMaintainedDormant
Setup difficultyeasymoderatehard
Complexity2/53/53/5
Audiencedeveloperpm founderdeveloper

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

How do you get it running?

Difficulty · easy Time to first run · 5min

Just add the library to your Go project with go get and start tagging struct fields.

No license information was provided in the explanation, so the permissions for using this code are unclear.

In plain English

When you create a new data object in Go, every field starts out empty or zero. That's fine for some cases, but often you want sensible starting values, a default name, a default age, a default configuration, without manually setting each one. This library solves that problem by letting you tag your data fields with default values and then automatically filling them in with a single command. You annotate each field with a small tag saying what the default should be, like default:"John Smith" on a name field or default:"27" on an age field. When you call the library's main function on your object, it reads those tags and populates everything for you. It handles simple values like text and numbers, but also more complex ones like lists, key-value maps, and nested objects. You can even use JSON snippets as defaults for complex fields, and it walks through nested structures recursively so defaults deep inside an object still get applied. One particularly useful feature is that it respects values you've already set. If you manually assigned a value to a field before calling the library, it won't overwrite that value with the default. It only fills in fields that are still at their empty starting state. There's also an opt-out tag for fields you want left alone entirely. Who would use this? Anyone building a Go application where objects need predictable starting states. A practical example: you have a configuration struct with thirty fields, most of which should default to sensible values but a few might be overridden by user input. Instead of writing boilerplate code to check and set each field, you tag them and call one function. The library also supports dynamic defaults through a custom interface, meaning if a field needs a value computed at runtime, like a random number or a timestamp, you can define custom logic for that.

Copy-paste prompts

Prompt 1
Show me how to use go-struct-defaults to set default values on a Go struct using field tags, including an example with a config struct that has string, int, and slice fields.
Prompt 2
I have a Go struct with nested objects and a field that needs a JSON snippet as its default value. How do I tag and apply defaults with this library?
Prompt 3
How do I use the custom interface in go-struct-defaults to set a dynamic default like a timestamp generated at runtime?
Prompt 4
Demonstrate how go-struct-defaults preserves values I already set on a struct and only fills in empty fields, plus how to use the opt-out tag for specific fields.

Frequently asked questions

What is go-struct-defaults?

A Go library that lets you tag struct fields with default values and automatically fills them in with a single function call, skipping any fields you've already set.

Is go-struct-defaults actively maintained?

Dormant — no commits in 2+ years (last push 2019-06-21).

What license does go-struct-defaults use?

No license information was provided in the explanation, so the permissions for using this code are unclear.

How hard is go-struct-defaults to set up?

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

Who is go-struct-defaults for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.