safe and easy casting from one type to another in Go
Go to file
blacktop ee94e109da
Merge 279833c6c5 into 48ddde5701
2024-06-10 02:24:51 +00:00
.github github: Replace the test workflow with the one used in Afero 2023-11-28 10:52:19 +01:00
.gitignore Remove expensive TRACE logging 2016-09-26 10:42:49 +02:00
LICENSE Initial commit 2014-04-03 11:21:16 -07:00
Makefile travis: Only check gofmt on Go 1.12 2019-05-31 11:32:28 +02:00
README.md docs: update badges 2023-08-04 11:09:44 +02:00
cast.go Add ToTimeInDefaultLocation/E 2021-07-27 13:05:07 +02:00
cast_test.go Merge remote-tracking branch 'upstream/master' 2023-05-16 11:14:24 -06:00
caste.go Merge remote-tracking branch 'upstream/master' 2023-05-16 11:14:24 -06:00
go.mod Bump Go version in go.mod to 1.19 2023-11-28 10:52:19 +01:00
go.sum deps: update to github.com/frankban/quicktest v1.14.6 2023-11-28 10:52:19 +01:00
timeformattype_string.go Adjust timezone logic 2021-07-27 13:05:07 +02:00

README.md

cast

GitHub Workflow Status PkgGoDev Go Version Go Report Card

Easy and safe casting from one type to another in Go

Dont Panic! ... Cast

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a number to a string, an interface into a bool, etc. Cast does this intelligently when an obvious conversion is possible. It doesnt make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast was developed for use in Hugo, a website engine which uses YAML, TOML or JSON for meta data.

Why use Cast?

When working with dynamic data in Go you often need to cast or convert the data from one type into another. Cast goes beyond just using type assertion (though it uses that when possible) to provide a very straightforward and convenient library.

If you are working with interfaces to handle things like dynamic content youll need an easy way to convert an interface into a given type. This is the library for you.

If you are taking in data from YAML, TOML or JSON or other formats which lack full types, then Cast is the library for you.

Usage

Cast provides a handful of To_____ methods. These methods will always return the desired type. If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

Cast also provides identical methods To_____E. These return the same result as the To_____ methods, plus an additional error which tells you if it successfully converted. Using these methods you can tell the difference between when the input matched the zero value or when the conversion failed and the zero value was returned.

The following examples are merely a sample of what is available. Please review the code for a complete set.

Example ToString:

cast.ToString("mayonegg")         // "mayonegg"
cast.ToString(8)                  // "8"
cast.ToString(8.31)               // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil)                // ""

var foo interface{} = "one more time"
cast.ToString(foo)                // "one more time"

Example ToInt:

cast.ToInt(8)                  // 8
cast.ToInt(8.31)               // 8
cast.ToInt("8")                // 8
cast.ToInt(true)               // 1
cast.ToInt(false)              // 0

var eight interface{} = 8
cast.ToInt(eight)              // 8
cast.ToInt(nil)                // 0