golang int conversion

王林
Release: 2023-05-16 10:03:38
Original
675 people have browsed it

Golang is a programming language that has attracted much attention in recent years. It uses concise syntax and fast compilation speed, and is suitable for concurrent programming and network programming. In Golang, we often need to perform data type conversion, the most common of which is integer (int) conversion. This article will explain in detail the int type conversion in Golang.

  1. Integer types in Golang

In Golang, there are four integer types:

  • int8: signed 8-bit Integer type, value range -128~127
  • int16: signed 16-bit integer type, value range-32768~32767
  • int32: signed 32-bit integer type, value range -2147483648~2147483647
  • int64: signed 64-bit integer, value range -9223372036854775808~9223372036854775807

In addition, Golang also provides the corresponding unsigned integer type, named The rule is uint plus the corresponding number of digits, such as uint8, uint16, uint32 and uint64.

  1. int type conversion to other types of data

Type conversion in Golang uniformly uses the toType() function, where the toType() function is the variable type that needs to be converted to, When converting, you need to ensure that the target type can fully accommodate the value range of the source type.

Below we take the conversion of int type to float32 and float64 types as an example:

2.1 Conversion of int to float32

Because the value range of float32 is larger than that of int, so The int type can be converted directly to float32. You can use the following code to achieve this:

func intToFloat32(i int) float32 {
    return float32(i)
}
Copy after login

2.2 Convert int to float64

In the same way, the int type can also be directly converted to float64. You can use the following code to achieve this:

func intToFloat64(i int) float64 {
    return float64(i)
}
Copy after login
  1. Convert other types of data to int type

In Golang, you can also use toType() to convert other types of data to int type. function, but be aware that if the value range of the source type is greater than the value range of the target type, precision may be lost during the conversion process.

Below we take the conversion of float32 and float64 types to int type as an example to illustrate:

3.1 Convert float32 to int

Because the precision may be lost during the conversion of float32 to int. , so it is best to make a judgment before conversion. You can use the following code to achieve it:

func float32ToInt(f float32) int {
    if f > math.MaxInt32 {
        return math.MaxInt32
    }
    if f < math.MinInt32 {
        return math.MinInt32
    }
    return int(f)
}
Copy after login

3.2 Converting float64 to int

For the same reason, converting float64 type to int type also requires precision judgment. , can be achieved using the following code:

func float64ToInt(f float64) int {
    if f > math.MaxInt32 {
        return math.MaxInt32
    }
    if f < math.MinInt32 {
        return math.MinInt32
    }
    return int(f)
}
Copy after login
  1. Summary

In Golang, integer type conversion is very common, but it should be noted that the target needs to be guaranteed during conversion The type can fully accommodate the value range of the source type, otherwise problems such as loss of precision may occur. If necessary, you can also customize functions for conversion implementation.

The above is the detailed content of golang int conversion. For more information, please follow other related articles on the PHP Chinese website!

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