validator for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

validator Library for Go: Complete Guide

\n\n

1. What It Is and Why You Need It

\n\n

The validator library (full package name — github.com/go-playground/validator/v10) is a powerful and flexible tool for validating structs and individual fields in the Go language. It allows you to declaratively describe data validation rules directly in struct tags, making the code clean, readable, and easily maintainable. Instead of writing dozens of lines of manual checks (if-else, switch), you simply add tags to fields, and the library does all the work for you.

\n\n

Why is this needed? In any web application, API, or service working with user input, it is critically important to validate data before processing it. An incorrect email, empty name, negative age, too long password — all of this can lead to logic errors, vulnerabilities (SQL injection, XSS), or server crashes. validator solves this problem elegantly: you define the rules once, and then call a single Validate.Struct() function, which checks all fields automatically.

\n\n

The library supports dozens of built-in validators (required, min, max, email, url, uuid, oneof, len, and many others), and also allows you to create your own custom validators. It is actively used in production, has excellent performance (thanks to reflection caching), and is compatible with popular frameworks such as Gin, Echo, and Fiber. If you write in Go and want to avoid boilerplate code, this library is a must-have.

\n\n

2. Installation

\n\n

The library is installed in the standard way via go get. Make sure you have Go version 1.18 or higher installed (1.21+ recommended).

\n\n
go get github.com/go-playground/validator/v10
\n\n

After executing the command, the package will be added to your go.mod. Import it in your code:

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

3. Quick Start — Minimal Working Example

\n\n

Let's create a simple user structure and validate its fields. This is a minimal example that demonstrates the core idea.

\n\n
package main\n\nimport (\n    "fmt"\n    "github.com/go-playground/validator/v10"\n)\n\n// User — structure with validation tags\ntype User struct {\n    Name  string `validate:"required,min=2,max=50"`\n    Email string `validate:"required,email"`\n    Age   int    `validate:"gte=0,lte=150"`\n}\n\nfunc main() {\n    // Create a validator instance\n    validate := validator.New()\n\n    // Test data\n    user := User{\n        Name:  "A",               // Name too short\n        Email: "invalid-email",   // Invalid email\n        Age:   -5,                // Negative age\n    }\n\n    // Perform validation\n    err := validate.Struct(user)\n    if err != nil {\n        // Iterate over all errors\n        for _, err := range err.(validator.ValidationErrors) {\n            fmt.Printf("Field: %s, Error: %s, Value: %v\\n",\n                err.Field(), err.Tag(), err.Value())\n        }\n    } else {\n        fmt.Println("Validation passed successfully!")\n    }\n}\n
\n\n

Program output:

\n
Field: Name, Error: min, Value: A\nField: Email, Error: email, Value: invalid-email\nField: Age, Error: gte, Value: -5
\n\n

As you can see, the library automatically finds all violations and returns structured information about each error.

\n\n

4. Main Methods and Functions

\n\n

Below are 8 key methods and functions of the library. For each one, the signature, description, and working example are provided.

\n\n

4.1. validator.New()

\n

Signature: func New() *Validate

\n

What it does: Creates a new validator instance. This is the main object through which

Recommendations