0Pricing
Go Academy · Lesson

Validation Libraries

Validate structs with tags.

Validation Libraries is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Validate Structs

Before using incoming data (from a form or API), you often must check it: required fields, valid email, value ranges. Validation libraries automate this using struct tags.

The validate Tag

Popular libraries like go-playground/validator read a validate tag describing rules per field.

A field might carry validate:"required,email".

An External Dependency

Validator libraries are third-party packages, not in the standard library. You add them with go get github.com/go-playground/validator/v10. They cannot run in a self-contained snippet here.

Defining Rules

Each field's validate tag lists rules separated by commas. Common rules: required, email, min, max, len.

Example Struct

A typical validated struct. (Conceptual; needs the validator package to run.)

type SignupForm struct {
	Email    string "validate:\"required,email\""
	Password string "validate:\"required,min=8\""
	Age      int    "validate:\"gte=18,lte=120\""
}

Running Validation

You create a validator instance and call Validate.Struct(form). It returns an error if any rule fails, or nil if all pass.

import "github.com/go-playground/validator/v10"

var validate = validator.New()

func check(f SignupForm) error {
	return validate.Struct(f)
}

Reading the Errors

On failure the returned error can be cast to validator.ValidationErrors, a slice describing each broken rule: which field and which constraint failed.

Common Built-in Rules

Handy rules include:

  • required - must not be zero value
  • email - valid email format
  • min / max - length or numeric bounds
  • gte / lte - numeric comparisons
  • oneof - must be one of a list

Validating Nested Structs

Validators recurse into nested structs automatically. A field that is itself a struct gets its own rules checked too, so whole object graphs are validated in one call.

Why Use a Library

Hand-writing validation for every field is tedious and error-prone. Tag-driven validation keeps rules declarative, next to the fields, and consistent across your app.

Manual Alternative

For simple cases you can validate by hand without a library; for many fields and complex rules a library wins. This runs anywhere since it uses only the standard library.

package main

import (
	"errors"
	"fmt"
	"strings"
)

func validateEmail(e string) error {
	if !strings.Contains(e, "@") {
		return errors.New("invalid email")
	}
	return nil
}

func main() {
	fmt.Println(validateEmail("bad"))
	fmt.Println(validateEmail("a@b.com"))
}

Quick Check

How does a validation library like go-playground/validator know which rules to apply to each field?

Recap

Validation libraries:

  • Read a validate tag to apply rules
  • Third-party (e.g. go-playground/validator), added via go get
  • Return errors listing each failed rule
  • Simple cases can be validated manually

Frequently asked questions

Is the “Validation Libraries” lesson free?

Yes — the full text of “Validation Libraries” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Validation Libraries”?

Validate structs with tags. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Validation Libraries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Struct Tags
  2. JSON Tags
  3. Validation Libraries
  4. Custom Tag Parsing
← Back to Go Academy