2014-04-04 02:21:16 +08:00
|
|
|
|
cast
|
|
|
|
|
====
|
2017-03-03 20:56:36 +08:00
|
|
|
|
[![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast)
|
2017-03-04 03:59:13 +08:00
|
|
|
|
[![Build Status](https://api.travis-ci.org/spf13/cast.svg?branch=master)](https://travis-ci.org/spf13/cast)
|
2017-03-03 20:56:36 +08:00
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast)
|
2014-04-04 02:21:16 +08:00
|
|
|
|
|
2020-07-02 11:43:16 +08:00
|
|
|
|
在Go中轻松安全地从一种类型转换为另一种类型
|
2014-04-06 03:18:23 +08:00
|
|
|
|
|
|
|
|
|
Don’t Panic! ... Cast
|
|
|
|
|
|
|
|
|
|
## What is Cast?
|
|
|
|
|
|
2020-07-02 11:43:16 +08:00
|
|
|
|
Cast是一个库,用于以一致且简单的方式在不同的go类型之间进行转换。
|
2014-04-06 03:18:23 +08:00
|
|
|
|
|
2020-07-02 11:43:16 +08:00
|
|
|
|
Cast提供简单的功能,可轻松将数字转换为字符串,进入布尔等接口。当明显转换是可能的。 它不会尝试猜测您的意思,例如,您只能在字符串为字符串时将其转换为int
|
|
|
|
|
int的表示形式,例如“ 8”。 Cast的开发目的是用于[Hugo](http://hugo.spf13.com), 使用YAML,TOML或JSON的网站引擎用于元数据。
|
2014-04-06 03:18:23 +08:00
|
|
|
|
|
|
|
|
|
## Why use Cast?
|
|
|
|
|
|
2020-07-02 11:43:16 +08:00
|
|
|
|
在Go中使用动态数据时,您通常需要强制转换或转换数据
|
|
|
|
|
从一种类型变成另一种类型。 强制转换不仅限于使用类型断言(尽管
|
|
|
|
|
它在可能的情况下使用它)来提供非常直接和方便的库。
|
2014-04-06 03:18:23 +08:00
|
|
|
|
|
2020-07-02 11:43:16 +08:00
|
|
|
|
如果您正在使用接口来处理诸如动态内容之类的内容您将需要一种简单的方法来将接口转换为给定类型。 这个是您的库。
|
|
|
|
|
如果您要从YAML,TOML或JSON或其他缺少格式的数据中获取数据完整类型,那么Cast是适合您的库。
|
2014-04-06 03:18:23 +08:00
|
|
|
|
|
|
|
|
|
## 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
|
2014-11-20 02:15:05 +08:00
|
|
|
|
the To_____ methods, plus an additional error which tells you if it successfully
|
2014-04-06 03:18:23 +08:00
|
|
|
|
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
|
|
|
|
|
|