Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
.gitignore | H A D | 18-Dec-2019 | 268 | |||
.travis.yml | H A D | 18-Dec-2019 | 188 | |||
LICENSE | H A D | 18-Dec-2019 | 1.1 KiB | |||
Makefile | H A D | 18-Dec-2019 | 1.1 KiB | |||
README.md | H A D | 18-Dec-2019 | 2.9 KiB | |||
cast.go | H A D | 18-Dec-2019 | 3.8 KiB | |||
cast_test.go | H A D | 18-Dec-2019 | 33.2 KiB | |||
caste.go | H A D | 18-Dec-2019 | 26.7 KiB | |||
go.mod | H A D | 18-Dec-2019 | 175 | |||
go.sum | H A D | 18-Dec-2019 | 521 |
README.md
1cast 2==== 3[![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast) 4[![Build Status](https://api.travis-ci.org/spf13/cast.svg?branch=master)](https://travis-ci.org/spf13/cast) 5[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast) 6 7Easy and safe casting from one type to another in Go 8 9Don’t Panic! ... Cast 10 11## What is Cast? 12 13Cast is a library to convert between different go types in a consistent and easy way. 14 15Cast provides simple functions to easily convert a number to a string, an 16interface into a bool, etc. Cast does this intelligently when an obvious 17conversion is possible. It doesn’t make any attempts to guess what you meant, 18for example you can only convert a string to an int when it is a string 19representation of an int such as “8”. Cast was developed for use in 20[Hugo](http://hugo.spf13.com), a website engine which uses YAML, TOML or JSON 21for meta data. 22 23## Why use Cast? 24 25When working with dynamic data in Go you often need to cast or convert the data 26from one type into another. Cast goes beyond just using type assertion (though 27it uses that when possible) to provide a very straightforward and convenient 28library. 29 30If you are working with interfaces to handle things like dynamic content 31you’ll need an easy way to convert an interface into a given type. This 32is the library for you. 33 34If you are taking in data from YAML, TOML or JSON or other formats which lack 35full types, then Cast is the library for you. 36 37## Usage 38 39Cast provides a handful of To_____ methods. These methods will always return 40the desired type. **If input is provided that will not convert to that type, the 410 or nil value for that type will be returned**. 42 43Cast also provides identical methods To_____E. These return the same result as 44the To_____ methods, plus an additional error which tells you if it successfully 45converted. Using these methods you can tell the difference between when the 46input matched the zero value or when the conversion failed and the zero value 47was returned. 48 49The following examples are merely a sample of what is available. Please review 50the code for a complete set. 51 52### Example ‘ToString’: 53 54 cast.ToString("mayonegg") // "mayonegg" 55 cast.ToString(8) // "8" 56 cast.ToString(8.31) // "8.31" 57 cast.ToString([]byte("one time")) // "one time" 58 cast.ToString(nil) // "" 59 60 var foo interface{} = "one more time" 61 cast.ToString(foo) // "one more time" 62 63 64### Example ‘ToInt’: 65 66 cast.ToInt(8) // 8 67 cast.ToInt(8.31) // 8 68 cast.ToInt("8") // 8 69 cast.ToInt(true) // 1 70 cast.ToInt(false) // 0 71 72 var eight interface{} = 8 73 cast.ToInt(eight) // 8 74 cast.ToInt(nil) // 0 75 76