Pitfalls of type conversion in Go language

introduce

As a strongly typed language, Go language often encounters type conversion scenarios when using Golang to develop projects. Integers can be directly converted, and byte slices and strings can also be directly converted.

However, if you do type conversion between integer and string, you need to use  strconv the functions provided by the standard library.


Standard library   type conversionstrconv

The Go language standard library  strconv provides some type conversion functions, such as the type conversion between integers and strings that are often used in project development.

func main() {
 salary := 5000
 salaryStr := strconv.Itoa(salary)
 fmt.Printf("%T salary=%d\n", salary, salary)
 fmt.Printf("%T salaryStr=%s\n", salaryStr, salaryStr)

 age := "23"
 ageInt, err := strconv.Atoi(age)
 fmt.Printf("%T age=%s\n", age, age)
 fmt.Printf("%T ageInt=%d err=%v\n", ageInt, ageInt, err)
}

Output result:

int salary=5000
string salaryStr=5000
string age=23
int ageInt=23 err=<nil>

Reading the above code, we use the standard library  strconv to convert an integer variable  salary to a string type variable  salaryStr; to  age convert a string type variable to an integer variable  ageInt.

However, readers and friends, have you found a problem? We use  strconv the functions provided by  the standard library Atoi to convert a string type variable into an integer variable, and what we get is  int the type. If we need to get a  int8 type of variable, we need to continue to do type conversion. For example:

age := "23"
ageInt, err := strconv.Atoi(age)
ageInt8 := int8(ageInt)

That is to say, if we need to convert a string type variable to a non-  int type integer variable, we need to do a second conversion, which is a little cumbersome to use in actual project development.

In addition, using the standard library  strconv for type conversion, in addition to being slightly cumbersome in some scenarios, there is another problem. Let's read the following piece of code first.

func main() {
  phoneNumber := "138001380001380013800013800138000"
 phoneNumberInt, err := strconv.Atoi(phoneNumber)
 fmt.Printf("%T phoneNumber=%s\n", phoneNumber, phoneNumber)
 fmt.Printf("%T phoneNumberInt=%d err=%v\n", phoneNumberInt, phoneNumberInt, err)
}

Output result:

string phoneNumber=138001380001380013800013800138000
int phoneNumberInt=9223372036854775807 err=strconv.Atoi: parsing "138001380001380013800013800138000": value out of range

Read the error message output by the above code  value out of range, which means that if the value we need to convert exceeds the value returned,  strconv the function provided by  the Go language standard library Atoi will return an error.

Therefore, when using functions  Atoi , we must do parameter verification and error handling.

Is there a simpler type conversion library? Next, let's take a look at the popular three-party libraries  cast.


Three-party library  cast type conversion

The three-party library for Go type conversion  cast is a library that is used more often. We use  cast  to handle the type conversion requirements of Part02. The code is as follows:

func main() {
  age2 := "23"
 age2Int8 := cast.ToInt8(age2)
 fmt.Printf("%T age2=%s\n", age2, age2)
 fmt.Printf("%T age2Int8=%d\n", age2Int8, age2Int8)

 phoneNumber2 := "138001380001380013800013800138000"
 phoneNumber2Int := cast.ToInt(phoneNumber2)
 fmt.Printf("%T phoneNumber2=%s\n", phoneNumber2, phoneNumber2)
 fmt.Printf("%T phoneNumber2Int=%d\n", phoneNumber2Int, phoneNumber2Int)
}

Output result:

string age2=23
int8 age2Int8=23
string phoneNumber2=138001380001380013800013800138000
int phoneNumber2Int=0

After reading the above code, we can find that  cast the variable of string type can be directly converted into the integer variable we need, and it is no longer cumbersome to use.

At the same time, it should be noted that if the conversion fails, a zero value of type will be returned, and when a variable of string type is  phoneNumber2 converted  cast to  int a variable of type, the result returned is  int the zero value of type.

 It's simpler to use  cast than to use  , and there's no need to handle errors. strconvHowever, cast there is another trap that we need to pay special attention to. Let's read the following piece of code first:

func main() {
  month := "07"
 monthInt8 := cast.ToInt8(month)
 fmt.Printf("%T month=%s\n", month, month)
 fmt.Printf("%T monthInt8=%d\n", monthInt8, monthInt8)

 month2 := "08"
 month2Int8 := cast.ToInt8(month2)
 fmt.Printf("%T month2=%s\n", month2, month2)
 fmt.Printf("%T month2Int8=%d\n", month2Int8, month2Int8)
}

Output result:

string month=07
int8 monthInt8=7
string month2=08
int8 month2Int8=0

Reading the output of the above piece of code, we can find that when using  cast the string type  month and  month2 converting it to an integer, the string is  "0" the month at the beginning, "07" after conversion, an integer is obtained  7, and  "08" after conversion, an integer is obtained  0.

We use  strconv conversion  again "08", the code is as follows:

func main() {
  month2 := "08"
 month2Int8 := cast.ToInt8(month2)
 fmt.Printf("%T month2=%s\n", month2, month2)
 fmt.Printf("%T month2Int8=%d\n", month2Int8, month2Int8)

 month2Int2, err := strconv.Atoi(month2)
 fmt.Printf("%T month2Int2=%d err=%v\n", month2Int2, month2Int2, err)
}

Output result:

int8 month2Int8=0
int month2Int2=8 err=<nil>

Readers can see from the output results that the integer "08" is obtained  strconv after conversion  8, so when we convert  "0" a string starting with one or more to an integer,  the conversion  cannot be used  "0" if the value after the string is greater   than When converting   a string starting with one or more characters into an integer, such as  , ,  etc., use   conversion instead of   conversion.7cast"0""08""009""00010"strconvcast


Summarize

In this article, we introduce two libraries for Go language type conversion, namely the standard library  strconv and the third-party library  cast, which are  cast more convenient and safer, but there are also pitfalls. We need to pay special attention to avoid falling into traps in project development.

For more usage of these two type conversion libraries, interested readers can read the manual carefully and practice more.

References:

strconv: https://pkg.go.dev/strconv

cast: https://github.com/spf13/cast

Guess you like

Origin blog.csdn.net/qq_54129105/article/details/132264733