In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how the go language converts types. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In the go language, type conversion is used to convert variables of one data type into variables of another type, with the syntax "type_name (expression)"; "type_name" is the type name, and "expression" is the expression, which is the value that needs to be converted.
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 a variable of one data type to another. 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, an integer is converted to a floating-point type, the result is calculated, and the result is assigned to a floating-point variable:
Package mainimport "fmt" func main () {var sum int = 17 var count int = 5 var mean float32 mean = float32 (sum) / float32 (count) fmt.Printf ("mean value is:% f\ n", mean)}
The output of the above example is as follows:
The value of mean is: 3.400000
Type conversion can only succeed if it is correctly defined, such as from a type with a smaller range of values to a type with a larger range of values (converting int16 to int32). Loss of precision (truncation) occurs when converting from a type with a wide range of values to a type with a smaller range of values (converting int32 to int16 or float32 to int).
Only variables of the same underlying type can be converted to each other (such as converting int16 types to int32 types), while variables of different underlying types can cause compilation errors (such as converting bool types to int types):
Package mainimport ("fmt"math") func main () {/ / output various value ranges 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) / / initialize a 32-bit integer value var an int32 = 1047483647 / / output variable in hexadecimal form and decimal value fmt.Printf ("int32: 0x%x% d\ n", a, a) / / converts the a variable value to hexadecimal Numeric truncation occurs b: = int16 (a) / / hexadecimal form of the output variable and decimal value fmt.Printf ("int16: 0x%x% d\ n", b, b) / / Save the constant as float32 type var c float32 = math.Pi / / convert to int type, floating-point precision loss fmt.Println (int (c)}
The code is as follows:
Line 11-14, which outputs a range of values for several common integer types.
Line 17, declare the variable an of type int32 and initialize it.
In line 19, use the% x verb of fmt.Printf to output the value in hexadecimal format, which outputs the 32-bit value of a before conversion.
Line 22 converts the value of a to the int16 type, that is, from a 32-bit signed integer to a 16-bit signed integer. Because the range of values of the int16 type is smaller than that of the int32 type, the value is truncated (loss of precision).
Line 24 outputs the converted value of variable a, that is, the value of b, which is also printed in both hexadecimal and decimal.
In line 27, math.Pi is the constant of the math package, which has no type by default and is automatically deduced from the actual type where it is referenced, where math.Pi is assigned to the 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 127int16 range:-32768 32767int32 range:-2147483648 2147483647int64 range:-9223372036854775808 9223372036854775807int32: 0x3e6f54ff 1047483647int16: 0x54ff 217593
According to the output, the range of the 16-bit signed integer is-32768 and 32767, while the value of the variable an is not in this range. 1047483647 the corresponding hexadecimal is 0x3e6f54ff, and when it is converted to int16, the length is halved, that is, the hexadecimal is cut in half to become 0x54ff, and the corresponding decimal value is 21759.
When a floating-point number is converted to an integer, the decimal part is removed, leaving only the integer part.
Thank you for reading! This is the end of this article on "how to convert types in go language". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.