Home >Backend Development >Golang >How to perform type conversion in go language

How to perform type conversion in go language

青灯夜游
青灯夜游Original
2022-01-21 11:29:177907browse

In the Go language, type conversion is used to convert variables of one data type into variables of another type. The syntax is "type_name(expression)"; "type_name" is the type name, "expression" is an expression, which is the value that needs to be converted.

How to perform type conversion in go language

The operating environment of this tutorial: windows10 system, GO 1.11.2, thinkpad t480 computer.

Go language type conversion

Type conversion is used to convert variables of one data type into variables of another type. The basic format of Go language type conversion is as follows:

type_name(expression)

type_name is the type and expression is the expression.

Example

In the following example, the integer type is converted into a floating point type, the result is calculated, and the result is assigned to a floating point variable:

package main
import "fmt"
func main() {
   var sum int = 17
   var count int = 5
   var mean float32
   
   mean = float32(sum)/float32(count)
   fmt.Printf("mean 的值为: %f\n",mean)
}

The output result of the execution of the above example is:

mean 的值为: 3.400000

Type conversion can only be successful when the definition is correct, such as converting from a type with a smaller value range to a type with a larger value range (convert int16 converted to int32). Precision loss (truncation) occurs when converting from a type with a larger range to a type with a smaller range (converting int32 to int16 or float32 to int).

Only variables of the same underlying type can be converted to each other (such as converting the int16 type to the int32 type). Compilation errors will occur when variables of different underlying types are converted to each other (such as converting the bool type to the int type). ):

package main
import (
        "fmt"
        "math"
)
func main() {
        // 输出各数值范围
        fmt.Println("int8 range:", math.MinInt8, math.MaxInt8)
        fmt.Println("int16 range:", math.MinInt16, math.MaxInt16)
        fmt.Println("int32 range:", math.MinInt32, math.MaxInt32)
        fmt.Println("int64 range:", math.MinInt64, math.MaxInt64)
        // 初始化一个32位整型值
        var a int32 = 1047483647
        // 输出变量的十六进制形式和十进制值
        fmt.Printf("int32: 0x%x %d\n", a, a)
        // 将a变量数值转换为十六进制, 发生数值截断
        b := int16(a)
        // 输出变量的十六进制形式和十进制值
        fmt.Printf("int16: 0x%x %d\n", b, b)
        // 将常量保存为float32类型
        var c float32 = math.Pi
        // 转换为int类型, 浮点发生精度丢失
        fmt.Println(int(c))
}

The code description is as follows:

  • Lines 11 to 14 output the numerical ranges of several common integer types.

  • Line 17, declare the variable a of type int32 and initialize it.

  • Line 19 uses the %x verb of fmt.Printf to output the value in hexadecimal format. This line outputs the 32-bit value of a before conversion.

  • Line 22, convert the value of a to int16 type, that is, convert from 32-bit signed integer type to 16-bit signed integer type. Due to the value range of int16 type The value range is smaller than the int32 type, so the value will be truncated (precision is lost).

  • Line 24 outputs the converted a variable value, which is the value of b, and is also printed in hexadecimal and decimal formats.

  • Line 27, math.Pi is a constant of the math package. It has no type by default. It will be automatically deduced based on the actual type where it is referenced. Here math.Pi is assigned to a variable. c, so the type is float32.

  • Line 29, convert float32 to int type and output.

The code output is as follows:

int8 range: -128 127
int16 range: -32768 32767
int32 range: -2147483648 2147483647
int64 range: -9223372036854775808 9223372036854775807
int32: 0x3e6f54ff 1047483647
int16: 0x54ff 21759
3

According to the output result, the range of 16-bit signed integer is -32768~32767, and the value of variable a, 1047483647, is not in this range Inside. The corresponding hexadecimal value of 1047483647 is 0x3e6f54ff. After converting to int16 type, the length is shortened by half, that is, it is cut in half in hexadecimal and becomes 0x54ff, and the corresponding decimal value is 21759.

When a floating point number is converted to an integer, the decimal part will be removed and only the integer part will be retained.

Recommended learning: Golang tutorial

The above is the detailed content of How to perform type conversion in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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