Go's standard library validates data with plain code: strconv for parsing numbers, net/mail, net/url and net/netip for structured values, regexp for patterns, unicode/utf8 for text, and encoding/json with DisallowUnknownFields for request bodies. There is no built-in tag-based validator, so the idiomatic approach is explicit checks that return errors, optionally combined with errors.Join to report every problem at once.
Data integrity goes one step further: checksums from hash/crc32 and crypto/sha256, authentication codes from crypto/hmac, and database transactions from database/sql. This article shows each technique with current Go code.
Validation starts at the boundary: convert text into a typed value and reject anything that does not parse. strconv reports the exact failure, including overflow.
Wrap errors with %w so callers can inspect them using errors.Is and errors.As.
Use the dedicated parsers instead of hand-written regular expressions.
| Value | Function | Notes |
|---|---|---|
mail.ParseAddress | Follows RFC 5322, accepts Name <[email protected]> forms | |
| URL | url.Parse, url.ParseRequestURI | Check Scheme and Host yourself |
| IP address | netip.ParseAddr | Preferred over net.ParseIP, comparable and allocation-free |
| UTF-8 text | utf8.ValidString | Rejects invalid byte sequences |
Parsing an address only proves it is well formed. It does not prove the mailbox exists; confirm that by sending a verification message.
Write a Validate method that returns all problems rather than stopping at the first. errors.Join (Go 1.20) combines them, and errors.Is still works on the result.
errors.Join returns nil when the slice is empty, so Validate returns nil for valid input. When you need field-level messages in an API response, define your own error type with a field map instead.
Third-party packages such as go-playground/validator add struct tags. They are optional and useful on large payloads, but the standard-library approach has no reflection cost and is easy to test.
encoding/json ignores unknown fields by default. For API input, turn that off and reject trailing data.
Compile regular expressions once at package level. Go's regexp uses RE2, so matching time is linear and safe against catastrophic backtracking, but it has no lookahead. Also wrap request bodies with http.MaxBytesReader to cap their size.
Validation checks shape. Integrity checks that data was not changed or corrupted.
hash/crc32 detects accidental corruption and is fast. It is not secure against tampering.crypto/sha256 gives a collision-resistant digest for verifying downloads and files.crypto/hmac authenticates a message with a shared secret. Compare results with hmac.Equal, which runs in constant time.database/sql transactions keep multi-step writes atomic: tx.Commit or tx.Rollback.Combine these with database constraints (NOT NULL, UNIQUE, foreign keys, CHECK). Application checks give friendly errors; constraints guarantee integrity even when a bug or another service skips them.
html/template, database/sql placeholders) rather than trying to sanitise input.go test -fuzz).No. The standard library supplies parsers and building blocks, and you write the rules yourself. Tag-based validation needs a third-party package.
Collect them in a slice and return errors.Join(errs...), or return a custom error type that holds a map of field names to messages.
Yes for performance, because RE2 matching is linear in input size. Still limit the input length, and prefer dedicated parsers for emails and URLs.
A checksum detects accidental changes and anyone can recompute it. An HMAC needs a secret key, so only key holders can produce a valid value.
Validate with strconv, net/mail, net/url, net/netip, strict JSON decoding and compiled regular expressions, and combine failures with errors.Join. Protect integrity with SHA-256, HMAC, database transactions and constraints.