[cast]-go type conversion component, how can it be so easy to use!

Introduction to Cast

What is Cast?

Cast is a library that converts between different go types in a consistent and simple way.

Cast provides simple functions that can easily convert numbers to strings, convert interfaces to bool types, and so on. When an obvious conversion is possible, Cast will perform this operation intelligently. It will not try to guess what you mean, for example, you can only convert a string to a string representation of int, such as "8". Cast was developed for Hugo, which is a website engine that uses YAML, TOML or JSON as metadata.

Why use Cast?

When dealing with dynamic data in Go, it is usually necessary to convert the data from one type to another. Casting is not just about using type assertions (although it uses type assertions when possible), it provides a very straightforward and convenient library.

If you are using interfaces to handle things such as dynamic content, then you will need an easy way to convert the interface to a given type. This is the library for you.

If you get data from YAML, TOML, or JSON, or other formats that lack a complete type, then Cast is the library for you.

How to use

Coercion provides some methods of To_____. These methods will always return the required type. If the provided input cannot be converted to this type, the 0 or nil value of the type is returned.

Cast also provides the same method of To_____E. These methods return the same results as the To_____ method, plus an additional error to tell you whether the conversion was successful. Using these methods, you can distinguish the difference when the input matches a zero value and the difference when the conversion fails to return a zero value.

Case

The following example is just an example of an existing example. Please see the complete code 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

main function

package main

import (
	"fmt"
	"reflect"

	"github.com/spf13/cast"
)

func main() {
	var foo interface{} = "one more time"
	box := cast.ToString(foo)
	fmt.Println(box)
	box = cast.ToString("3.12021")
	fmt.Println(box)

	cvIntBox := cast.ToInt(8)
	fmt.Println(cvIntBox, reflect.TypeOf(cvIntBox))
	cvFloatBox := cast.ToFloat32(8.31)
	fmt.Println(cvFloatBox, reflect.TypeOf(cvFloatBox))
	cvBoolBox := cast.ToBool(true)
	fmt.Println(cvBoolBox, reflect.TypeOf(cvBoolBox))
}

Output

one more time
3.12021
8 int
8.31 float32
true bool

Data list

https://github.com/spf13/cast

Guess you like

Origin blog.csdn.net/HapplyFox/article/details/114011042