MCPcopy
hub / github.com/spf13/cast

github.com/spf13/cast @v1.10.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.10.0 ↗
196 symbols 409 edges 21 files 88 documented · 45%
README

cast

GitHub Workflow Status go.dev reference GitHub go.mod Go version OpenSSF Scorecard

Easy and safe casting from one type to another in Go

Don’t 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 doesn’t 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 you’ll 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

License

The project is licensed under the MIT License.

Extension points exported contracts — how you extend this code

Number (Interface)
Number is a type parameter constraint for functions accepting number types. It represents the supported number types th
number.go
Basic (Interface)
Basic is a type parameter constraint for functions accepting basic types. It represents the supported basic types this
cast.go

Core symbols most depended-on inside this repo

indirect
called by 11
indirect.go
trimZeroDecimal
called by 10
number.go
resolveAlias
called by 6
alias.go
Float64
called by 5
number.go
ToTimeInDefaultLocationE
called by 5
time.go
HasTimezone
called by 5
internal/time.go
ToStringE
called by 4
basic.go
ToTimeE
called by 4
time.go

Shape

Function 142
TypeAlias 32
Struct 9
Interface 7
Method 6

Languages

Go100%

Modules by API surface

zz_generated.go45 symbols
number.go32 symbols
cast_test.go18 symbols
alias_test.go18 symbols
map.go10 symbols
slice_test.go9 symbols
time_test.go8 symbols
map_test.go8 symbols
basic_test.go8 symbols
number_test.go7 symbols
time.go5 symbols
number_internal_test.go5 symbols

Dependencies from manifests, versioned

github.com/dave/jenniferv1.7.1 · 1×
github.com/frankban/quicktestv1.14.6 · 1×
github.com/kr/prettyv0.3.1 · 1×
github.com/kr/textv0.2.0 · 1×
github.com/rogpeppe/go-internalv1.9.0 · 1×

For agents

$ claude mcp add cast \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact