Cast is a Go library that provides safe and easy type conversion between different Go types.
Cast solves the problem of converting between types when working with dynamic data, particularly when that data comes from untyped sources like YAML, TOML, or JSON. Rather than relying on Go's type assertions alone, Cast provides a set of conversion functions that intelligently handle common conversions. For example, it can convert a string representation of a number into an actual integer, or convert various types to strings. The library takes a conservative approach: it only performs conversions when the transformation is unambiguous, and it returns zero or nil values rather than guessing at intent when conversion is impossible.
Cast is well-suited for projects that consume or manipulate dynamic data from configuration files or other untyped sources. It provides two families of conversion methods: the To_____ functions that return a value and silently return zero values on failure, and the To_____E variants that also return an error indicating whether the conversion succeeded. This design lets developers choose between convenience and explicit error handling depending on their needs. The library was developed for use in Hugo, a static site generator, which reflects its origin in handling metadata from multiple formats.
The project experiences slow response times to issues and pull requests, with first responses often taking weeks or longer.