SoFunction
Updated on 2025-03-05

Detailed explanation of the use of Golang language minimalist type conversion library cast

01 Introduction

In Golang language project development, because the Golang language is strongly typed, type conversion is often used. In this article, we introduce the type conversion three-party library - /spf13/cast, which is a minimalist type conversion three-party library. Through the functions it provides, we can facilitate type conversion, greatly improving our development efficiency.

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

In fact, when we need to convert between various integers and floating point types, we can use cast type conversion; when converting between various integers and strings, we can use the standard library strconv operation, but the usage method is not elegant enough. Moreover, if the value you need to convert is an interface type, you need to first assert the type and then perform type conversion, which all seems more complicated. cast can make these tasks simpler and make our code more elegant.

Although cast is relatively simple to use and provides some () and () functions, we will introduce the usage of cast through some simple examples, such as converting to string types with relatively high frequency.

02 Convert to string type

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

Sample code:

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

Output result:

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 empty string
val= type=string // The value is empty string

Reading the above code, we can find that the output results of the last two lines of code are empty strings, but in fact this is not the case. We can use the () function to convert the parameter f and look at the return result.

Sample code:

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

Output result:

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

Reading the above code, we can find that the same given parameters use different functions ( () and () ) and the result is different.

Reading the cast source code, we can find that the underlying implementation of () is called (), but the error returned by () is ignored.

Source code:

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

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

03 Summary

In this article, we introduce the minimalist type conversion three-party library cast, which can greatly improve our development efficiency and make our code more elegant, helping us operate type conversion more easily and safely.

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

This is the end of this article about Golang language minimalist type conversion library cast. For more related Golang type conversion library cast content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!