Home >Backend Development >Golang >What are the methods of type conversion in Go language?
As the Go language becomes increasingly popular in the Internet industry, more and more developers are beginning to get involved in this language. In the Go language, type conversion is also one of the common programming operations. This article will introduce some common type conversion methods.
Type assertion is an operation that converts an interface type to another type. In the Go language, type assertions can be made using the following syntax:
value, ok := interface{}.(type)
where value represents the converted value, ok represents whether the type assertion is successful, interface{} represents the variable to be converted, .(type) represents The type to convert to. The following is an example:
var x interface{} x = "Hello World" s, ok := x.(string) if ok { fmt.Println(s) } else { fmt.Println("x is not a string") }
In the above code, we first create an empty interface variable x, and then assign it to the string "Hello World". Then, we assert x as a string type and assign the result to the variable s, and finally use the ok variable to determine whether the assertion was successful.
The strconv package built into the Go language provides some functions for converting between strings and basic data types. The following are some commonly used strconv functions:
Atoi(s string) (int, error)
: Convert string to int type Itoa(i int) string
: Convert int type to string typeParseBool(str string) (value bool, err error)
: Convert string to bool type ParseInt(s string, base int, bitSize int) (i int64, err error)
: Convert the string to int64 type, base represents the base number, bitSize represents the bit of the variable NumberParseFloat(s string, bitSize int) (float64, error)
: Convert the string to float64 type, bitSize represents the number of digits in float64 FormatFloat(f float64, fmt byte, prec, bitSize int) string
:Convert float64 type to string typeFormatInt(i int64, base int) string
:Convert int64 Type conversion to string typeThe following is an example:
s := "100" i, err := strconv.Atoi(s) if err != nil { fmt.Println("conversion error:", err) } else { fmt.Println(i) }
In the above code, we use the Atoi function in the strconv package to convert the string "100" to an integer , and then determine whether the conversion is successful through error judgment.
If you need to convert one type of variable to another type of variable in a Go language program, you can use forced type conversion. The following is an example:
var x float64 = 3.14 var y int = int(x) fmt.Println(y)
In the above code, we first assign the value of the floating-point variable x to 3.14, then convert it to the integer variable y, and print out the value of y.
Summary
This article introduces the type conversion methods in the Go language, including type assertions, strconv package and forced type conversion. Type conversion is a common operation in programming. Mastering these type conversion methods can help improve programming efficiency.
The above is the detailed content of What are the methods of type conversion in Go language?. For more information, please follow other related articles on the PHP Chinese website!