finds.dev← search

// the find

spf13/cast

★ 3,978 · Go · MIT · updated Apr 2026

safe and easy casting from one type to another in Go

cast is a type-conversion utility for Go, born out of Hugo's need to handle YAML/TOML/JSON data where types are loose or absent. It wraps Go's type assertion with fallback logic so you get a zero value instead of a panic when a conversion fails. The target audience is anyone parsing config files or dynamic data in Go.

The dual API design — To<Type>() for 'give me something' and To<Type>E() for 'tell me if it worked' — is the right call for this problem space. The generated code (zz_generated.go, the generator/ subpackage) keeps the repetitive type-matrix code maintainable rather than hand-authored. It's been battle-tested inside Hugo at scale, which is a meaningful signal. The codebase is tiny and readable — you can audit the full conversion logic in an afternoon.

The silent zero-value fallback in the non-E methods is a footgun: cast.ToInt("banana") returns 0 with no indication anything went wrong, which makes bugs hard to trace. There's no support for generics — this was written before Go 1.18 and the API hasn't been updated, so you're back to interface{} everywhere when Go now has tools to do this more cleanly. The time parsing logic (internal/time.go) supports a fixed list of formats, so unusual but valid timestamps will silently produce a zero time. No support for custom converters — if your type isn't in the built-in set, you're on your own.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →