cast returns zero values on failure unless you call the E variant, and it will not guess what you meant
safe and easy casting from one type to another in Go
At a glance
- What is it?
- spf13/cast converts between Go types for dynamic data from YAML, TOML and JSON. Every ToX function has a ToXE twin, and the choice between them decides whether a failed conversion is visible.
- Who is it for?
- Use cast when you read loosely typed data from YAML, TOML or JSON and want one consistent set of conversion functions. Choose the ToXE variants whenever a zero is a legitimate value, because the plain functions hide failure.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 78 days ago.
- What is it written in?
- Mainly Go, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.
Star history
- spf13/cast3,983 ★ · +2
| Repository | 2026-10-08 | 2026-11-25 |
|---|---|---|
| spf13/cast | 4k | 4k |
2 Stars in the last 48 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
ToX returns a zero value on failure, so 0 can mean two things
cast offers a family of To_____ functions, and each one always returns the type you asked for. If the input will not convert, you get the 0 or nil value for that type. There is no panic and no error value to inspect, and the README opens with the line Don't Panic! to make that point.
The cost is ambiguity. ToInt of the string 0 returns 0, and ToInt of the string hello also returns 0. The caller cannot tell a real zero from a failed conversion.
A config loader that reads a port number with ToInt will silently get port 0 when the value is a malformed string, and the program carries on. Whenever zero is a valid value in your data, or a missing value would be a bug, this behavior hides the fault until something downstream fails.
ToXE adds the error that separates a zero from a failure
Every To_____ function has an identical To_____E twin. It returns the same result plus an error that tells you whether the conversion succeeded. With the E variants you can distinguish input that matched the zero value from input that failed and produced the zero value as a fallback.
In practice the pair gives you two coding styles. Use ToString, ToInt and the others in display code, templating and places where a default is acceptable. Use ToStringE, ToIntE and the others at trust boundaries, where bad input must be rejected or logged.
The README says the examples are a sample of what is available and tells you to review the code for the complete set. The full list of converters is therefore not on the front page. The root of the repository holds the real surface in files such as basic.go, number.go, slice.go, map.go and time.go.
It converts only when the conversion is obvious
The README states the rule directly. Cast does its work intelligently when an obvious conversion is possible, and it makes no attempt to guess what you meant. Its example is that a string converts to an int only when the string is the representation of an int, such as the text 8.
That sets a boundary. A string like eight, or 8 apples, is not an obvious conversion, so it falls through to the zero value or to an error from the E variant.
The ToString examples show what is covered on the output side:
cast.ToString("mayonegg") // "mayonegg"
cast.ToString(8) // "8"
cast.ToString(8.31) // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil) // ""Note that nil becomes an empty string, not an error through the plain function.
ToInt truncates floats and maps booleans to 1 and 0
The ToInt examples show behavior that a reader might not expect from a conversion library:
cast.ToInt(8) // 8
cast.ToInt(8.31) // 8
cast.ToInt("8") // 8
cast.ToInt(true) // 1
cast.ToInt(false) // 0The float 8.31 becomes 8, so the fractional part is dropped without a signal in the plain function. A boolean converts to 1 or 0. An interface holding 8 converts to 8, and nil converts to 0.
If you round money or measurements through ToInt, you lose data silently. Rounding is the caller's job before the cast. Whether the E variant reports an error for a float with a fractional part is not stated in the README, so read the code or write a test for that case.
It exists to tame untyped YAML, TOML and JSON values
cast was developed for use in Hugo, the website engine, which reads metadata from YAML, TOML or JSON. Those formats lack full types, so a value that looks like a number in one file may arrive as a string in another, and a decoded document is full of interface values.
The README makes the case in two sentences. When you work with dynamic data in Go, you need to move data from one type to another. cast goes beyond a plain type assertion, though it uses assertion when it can.
Two audiences are named: people handling interfaces for dynamic content, and people taking in data from formats that lack full types. If your structs are already typed and decoded by a standard library, cast adds little. The README claims no struct-to-struct conversion, and it does not make a claim about maps or slices beyond what its file names suggest.
The module needs Go 1.21 and runs its tests with the race detector
go.mod declares the module github.com/spf13/cast with go 1.21.0. The only direct requirement is github.com/frankban/quicktest v1.14.6, a test helper, with go-cmp, kr/pretty, kr/text and go-internal as indirect entries. A library consumer therefore adds no runtime dependency beyond the module itself.
Development uses just. The justfile defines test as go test -shuffle on -race -v ./..., lint as golangci-lint run, fmt as golangci-lint fmt, and check as test plus lint. The README commands are just test, just lint and just fmt, and some linter violations are fixed automatically by the last one. For a smoother setup the README recommends installing devenv and direnv, and the repository holds devenv.nix, devenv.yaml, devenv.lock and .envrc.
There is also a generator/ directory and a zz_generated.go file in the root, which indicates some conversion code is generated.
Releases are a year old while the repository still moves
The newest release is v1.10.0 on 2025-09-08, after v1.9.2 on 2025-06-02 and v1.9.1 on 2025-06-01. The repository is not archived, and its last push was 2026-09-09. Code has moved after the last tag, so pinning to v1.10.0 gives you the released behavior and not the master branch.
The README contains no install command, so there is no snippet to quote. The module path is github.com/spf13/cast, shown by go.mod and by the pkg.go.dev badge, and that is the import path.
The license is MIT. Related projects people search for next to it, such as viper and afero, come from the same author, but this repository does not document how they use cast.
Editorial conclusion
Use cast when you read loosely typed data from YAML, TOML or JSON and want one consistent set of conversion functions. Choose the ToXE variants whenever a zero is a legitimate value, because the plain functions hide failure. Skip it for strict parsing or for struct-to-struct mapping, which the README never claims, and check the code for any conversion you depend on, since the README samples only a few.
Frequently asked questions
Does Go have type casting, and what does cast add?
Go has type assertion, and cast goes beyond it while using assertion when possible. It converts values such as numbers to strings and interfaces to bools when an obvious conversion exists.
How do you type cast with spf13/cast?
Call a To_____ function such as cast.ToInt or cast.ToString. Use the To_____E variant, such as cast.ToIntE, if you also want an error telling you whether the conversion succeeded.
What does cast mean in programming, in this library?
Here cast is a library that converts between different Go types in a consistent and easy way. It only converts when an obvious conversion is possible and does not guess what you meant.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/spf13-cast)Star history chart for your README
[](https://hysenlabs.com/projects/spf13-cast)Was this useful?