In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
What are the data types in the Go language and how to convert them? most people do not understand the knowledge points of this article, so the editor summarizes the following content, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Go language data types and how to convert between types" article.
1. Basic data types
There are many data types, let's first study the basic ones, such as Boolean type, numeric type, string type.
The number types are uint8, uint16, uint32, uint64, int8, int16, int32, int64 (uint and int differ in that uint is an unsigned integer, that is, only positive numbers are supported, not negative numbers)
Digital floating-point types include fload32, float64, complex64 and complex126 (the latter two are both real and imaginary types, so let's not study them for the time being)
The sample code is as follows:
/ / data type func test_data () {var a bool var b int16 var c uint64 = 1231254353452 var d float32 = 1.78 fmt.Println (a) fmt.Println (b) fmt.Println (c) fmt.Println (d) fmt.Println ("int8 range:", math.MinInt8, math.MaxInt8) fmt.Println ("uint8 range:", math.MaxUint8)}
Execution result
False
0
1231254353452
1.78
Int8 range:-128 127
Uint8 range: 255
Be careful
1. If the underlying data type variable that is not assigned has an initial value, unlike Java, which defaults to null. The initial value of the bool type is false,int and the initial value is 0.
2. The value ranges of uint and int are different, and uint is only a positive number.
2. Basic data type conversion
The problem of data type conversion is often encountered in the project, so test it.
The sample code is as follows
/ / data type conversion func test_convert () {var num int = 10 var num1 float32 = float32 (num) var num2 float64 = float64 (num) fmt.Printf ("num type is% TMagn1 type% Tnum2 type% T\ n", num, num1, num2)}
Execution result
Num type is int,num1 type is float32,num2 type is float64
Pay attention to the use of formatted verbs in Printf, look for them and sort them out.
The verb function% v outputs% + v according to the original value of the value, expands the structure field name and value on the basis of% v, outputs Go language syntax format value% T output Go language data type%% output ontology% b output integer type% o output in octal format integer% d output in decimal format integer% x output in hexadecimal format Lowercase% X outputs integers in hexadecimal form, uppercase% U outputs% f in Unicode character mode,% p pointer in floating point mode, hexadecimal mode shows 3, and basic data types are converted to strings.
This is also a scenario that is often used in projects.
The sample code is as follows, first using the Sprintf method of fmt to transform.
/ / the basic data type is changed to stringfunc test_convert_string () {var an int = 10 var b float64 = 1.8 var c bool = true var d byte ='d' var e uint64 = 8 var str string fmt.Println (str + "a") str= fmt.Sprintf ("% d", a) fmt.Printf ("str type is% TMagneString% v\ n", str Str) str= fmt.Sprintf ("% f", b) fmt.Printf ("str type is% Tjurisdiction stringed% v\ n", str, str) str= fmt.Sprintf ("% t", c) fmt.Printf ("str type is% Tjorie strung% v\ n", str, str) str= fmt.Sprintf ("% c", d) fmt.Printf ("str type is% Trectory% v\ n", str Str) str= fmt.Sprintf ("% b", e) fmt.Printf ("str type is% Tjurisdiction stringing% v\ n", str, str)}
Execution result
A
Str type is string,str=10
Str type is string,str=1.800000
Str type is string,str=true
Str type is string,str=d
Str type is string,str=1000
Be careful
1. You can see that if there is no initial copy of the string type, it defaults to an empty string.
4. The use of strconv
Using strconv, you can perform string conversion on various data types.
Examples are as follows
/ / the use of strconv func test_strconv () {var i int = 10 var x int = 999 var f float64 = 1.8 var t bool = false var z int = 97 var str string / / decimal int str= strconv.FormatInt (int64 (I), 10) fmt.Printf ("str type is% TJournal stringing% v\ n", str Str) / / hexadecimal int str= strconv.FormatInt (int64 (x), 16) fmt.Printf ("str type is% TJournal stringing% v\ n", str, str) / / floating point type F for format, 3 for number of digits 64 means to convert to float64 str= strconv.FormatFloat (float64 (f), 'fending, 3,64) fmt.Printf ("str type is% Tjingle% v\ n", str, str) / / Boolean type str= strconv.FormatBool (t) fmt.Printf ("str type is% Tjingle% v\ n", str Str) / / string function distinguishes var str1= strconv.Itoa (z) var str2= string (z) fmt.Printf ("str1 type is% TForce str1% v\ n", str1, str1) fmt.Printf ("str2 type is% TForce str1% v\ n", str2, str2)}
Execution result
Str type is string,str=10
Str type is string,str=3e7
Str type is string,str=1.800
Str type is string,str=false
Str1 type is string,str1=97
Str2 type is string,str2=a
Be careful
1. FormatInt parameters can be selected as decimal or hexadecimal.
2. You can choose to keep a few decimal places for the FormatFloat parameter.
3. The Itoa method can convert int to string, but the string method converts a number into a character whose ASCII code value is equal to the integer number.
5. Change the string to the basic type
Use strconv to convert a string to a base type.
The sample code is as follows
/ / the string is converted to the base type func test_string_convert () {var str string = "true" b, _: = strconv.ParseBool (str) fmt.Printf ("b type is% Trecedence% v\ n", b, b) str = "111" / / returns int64 I by default, _: = strconv.ParseInt (str, 10) 64) fmt.Printf ("I type is% Tpenny% v\ n", I, I) str = "1.908" f, _: = strconv.ParseFloat (str, 64) fmt.Printf ("f type is% Tgravity% v\ n", f, f) str = "abc" c, C1: = strconv.ParseInt (str, 10,64) fmt.Printf ("c type is% T" Centering% v\ n ", c, c) fmt.Printf (" C1 type is% TMagne C1% v\ n ", C1, C1)}
Execution result
B type is bool,b=true
I type is int64,i=111
F type is float64,f=1.908
C type is int64,c=0
C1 type is * strconv.NumError,c1=strconv.ParseInt: parsing "abc": invalid syntax
Be careful
1. ": =" equals to declaring variables plus assignments.
2. The int type returned by ParseInt is int64,ParseFloat, and the float type returned by float64 is float64.
3. There are two values returned. If parsing fails, range the initialization value of the data type, for example: the int type returns 0. The second value is an exception, or null if there is no exception.
The above is about the content of this article on "what are the data types in Go language and how to convert them?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.