What are the methods of type conversion in Go language?

王林
Release: 2023-06-11 09:05:06
Original
1057 people have browsed it

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.

  1. Type assertion

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)
Copy after login

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")
}
Copy after login

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.

  1. strconv package

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 type
  • ParseBool(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 Number
  • ParseFloat(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 type
  • FormatInt(i int64, base int) string:Convert int64 Type conversion to string type

The following is an example:

s := "100"
i, err := strconv.Atoi(s)
if err != nil {
    fmt.Println("conversion error:", err)
} else {
    fmt.Println(i)
}
Copy after login

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.

  1. Forced type conversion

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template