How to ensure required fields in struct consistently?

This page summarizes the projects mentioned and recommended in the original post on /r/golang

SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  1. validator

    :100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

    Use https://github.com/go-playground/validator, which would just simply require you to add a tag to your struct for those fields.

  2. SaaSHub

    SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives

    SaaSHub logo
  3. semgrep

    Lightweight static analysis for many languages. Find bug variants with patterns that look like source code.

    FWIW you can probably write a semgrep rule or something, to find all struct literals which don't mention a specific field.

  4. mapstructure

    Discontinued Go library for decoding generic map values into native Go structures and vice versa.

    I'm doing it by validating a map[string]any first then putting it into a structure using mapstructure. It covers most use-cases and offers the most flexibility, at the expense of a bit of performance.

  5. go-exhaustruct

    golang analyzer that finds structures with uninitialized fields

    Have you consider adding a linter as part of you build process to detect those un-initialized fields? Like go-exhaustruct, (it's already included in golangci-lint if you use that already)

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • Go 1.25: JSON v2 e Novo GC

    3 projects | dev.to | 20 Aug 2025
  • Go Performance: Pequenas mudanças que ajudam a melhorar o desempenho do seu app

    3 projects | dev.to | 30 Jul 2024
  • Protoc Plugins with Go

    2 projects | dev.to | 19 Aug 2023
  • Question about Unmarshalling

    3 projects | /r/golang | 19 Jul 2022
  • Protobuf - Go support for Google's protocol buffers

    1 project | /r/github_trends | 27 May 2022

Did you know that Go is
the 4th most popular programming language
based on number of references?