Golang language minimal type conversion library cast

01

introduce

In the development of Golang language project, because Golang language is strongly typed, conversion to type is often used. In this paper, we introduce the type conversion tripartite Library - github.com/spf13/cast, which is a minimal type conversion tripartite library. Through the functions it provides, we can facilitate type conversion and greatly improve our development efficiency.

Moreover, cast automatically performs correct operations according to certain rules. For example, when we use cast.ToInt() to convert a string to an integer, it will convert the parameter to an integer only when the parameter is an int string, such as "4", otherwise it will be converted to an integer zero value.

In fact, when we need to convert between various integer and floating-point types, we can use cast; When converting between various integers and strings, you can use the standard library strconv operation, but it is not elegant enough. Moreover, if the value to be converted is an interface type, you need to first type assert and then type convert, which is more complex. Cast can make this simple and make our code more elegant.

Although the use of cast is relatively simple. It provides some cast.ToXxx() and cast. Toxxe() functions, we introduce the use of cast through some simple examples, such as conversion to string type with relatively high frequency.

02

Convert to string type

We can use cast.ToString() function to convert the given parameter to string type. If the given parameter cannot be converted to string type, it will return type zero value (string type zero value - empty string).

Example code:

a := 1
fmt.Printf("val=%v type=%T\n", cast.ToString(a), cast.ToString(a))
b := 3.14
fmt.Printf("val=%v type=%T\n", cast.ToString(b), cast.ToString(b))
c := "hello"
fmt.Printf("val=%v type=%T\n", cast.ToString(c), cast.ToString(c))
d := []byte("golang")
fmt.Printf("val=%v type=%T\n", cast.ToString(d), cast.ToString(d))
var e interface{} = "frank"
fmt.Printf("val=%v type=%T\n", cast.ToString(e), cast.ToString(e))
f := []int{1, 2, 3}
fmt.Printf("val=%v type=%T\n", f, f)
fmt.Printf("val=%v type=%T\n", cast.ToString(nil), cast.ToString(nil))

Output results:

val=1 type=string
val=3.14 type=string
val=hello type=string
val=golang type=string
val=frank type=string
val= type=string // The value is an empty string
val= type=string // The value is an empty string

After reading the above code, we can find that the output result of the last two lines of code is an empty string. In fact, this is not the case. We can use the cast.ToStringE() function to convert the parameter f and take a look at the returned result.

Example code:

v, err := cast.ToStringE([]int{1,2,3})
if err != nil {
  fmt.Println(err)
  return
}
fmt.Printf("val=%v type=%T\n", v, v)

Output results:

unable to cast []int{1, 2, 3} of type []int to string

Reading the above code, we can find that the same given parameters and different functions (cast.ToString() and cast.ToStringE()) get different return results.

After reading the source code of cast, we can find that the underlying implementation of cast.ToString() calls cast.ToStringE(), but ignores the error returned by cast.ToStringE().

Source code:

// ToString casts an interface to a string type.
func ToString(i interface{}) string {
 v, _ := ToStringE(i)
 return v
}

We can use the cast. Toxxe() function to determine whether the type zero value obtained after conversion is an error.

03

summary

In this paper, we introduce the minimal type conversion tripartite library cast, which can greatly improve our development efficiency, make our code more elegant, and help us operate type conversion more easily and safely.

In this article, we briefly introduce the use of cast through the use of cast.ToString() function. In addition, it also supports many other types. Limited to space, we do not repeat them one by one. Interested readers and friends, it is recommended to read the official documents or source code to learn more.

Posted by forgun on Wed, 17 Nov 2021 20:54:09 -0800