In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Golang language minimalist type conversion library cast use case analysis", in daily operation, I believe many people in Golang language minimalist type conversion library cast use case analysis problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Golang language minimalist type conversion library cast use case analysis" doubts helpful! Next, please follow the small series to learn together!
01 Introduction
In Golang project development, because Golang is strongly typed, type conversions are often used. In this article, we introduce the three-party library of type conversion-github.com/spf13/cast, which is a three-party library of minimal type conversion. Through the functions it provides, it can facilitate our type conversion and greatly improve our development efficiency.
Also, cast automatically performs the correct operation according to certain rules, for example, when we use cast.ToInt() to convert a string to an integer, it will only convert the argument to an integer if the argument is an int string, such as "4", otherwise it will convert to an integer zero value.
In fact, when we need to convert between various integers and floating-point types, we can use mandatory type conversion; when converting between various integers and strings, we can use the standard library strconv operation, but the way we use it is not elegant enough. Also, if the value to be converted is an interface type, you need to make type assertions first and then type conversions, all of which are more complicated. Cast can make these tasks easier and make our code more elegant.
Although cast is relatively simple to use, and it provides some cast.ToXxx() and cast.ToXxxE() functions, we'll introduce the use of cast through some simple examples, such as the relatively frequent use of conversions to string types.
02 Convert to string type
We can use the cast.ToString() function to convert a given argument to a string type, and if the given argument cannot be converted to a string type, it will return a type zero value (string type zero value-empty string).
Example code:
a := 1fmt.Printf("val=%v type=%T\n", cast.ToString(a), cast.ToString(a))b := 3.14fmt.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:
val=1 type=string
val=3.14 type=string
val=hello type=string
val=golang type=string
val=frank type=string
val= type=string //empty string
val= type=string //empty string
Reading the above code, we can see that the output 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 transform the parameter f and 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:
unable to cast []int{1, 2, 3} of type []int to string
Reading the code above, we can see that different functions (cast.ToString() and cast.ToStringE()) return different results for the same given parameter.
Reading the cast source code, we can see that the underlying implementation of cast.ToString() is to call cast.ToStringE(), but the error returned by cast.ToStringE() 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 cast.ToxxxE() function to determine whether the resulting type zero value is wrong.
At this point, the study of "Golang language minimalist type conversion library cast use case analysis" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.