Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the data types of Go language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about the data types of Go language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Basic data type description

Type description uint32-bit or 64-bit uint8 unsigned 8-bit integer (0 to 65535) uint16 unsigned 16-bit integer (0 to 65535) uint32 unsigned 32-bit integer (0 to 4294967295) uint64 unsigned 64-bit integer (0 to 18446744073709551615) int32-bit or 64-bit int8 signed 8-bit integer int16 signed 16-bit integer (- 32768 to 32767) int32 signed 32-bit integer (- 2147483648 to 2147483647) int64 signed 64 Aliases for bit integers (- 9223372036854775808 to 9223372036854775807) byteuint8 (type byte = uint8) aliases for runeint32 (type rune = int32) Represents a unicode code uintptr unsigned integer that holds a pointer that is an unsigned integer type that does not specify a specific bit size but is large enough to hold the pointer.

Uintptr types are only needed in underlying programming, especially where the Go language interacts with C function libraries or operating system interfaces. Float32IEEE-754 32-bit floating-point numbers float64IEEE-754 64-bit floating-point numbers complex6432 bits real and imaginary complex12864 bits real and imaginary numbers

Integer type

Integer data can be divided into two types, signed and unsigned.

Signed: int, int8, int16, int32, int64

Unsigned: uint, uint8, uint16, uint32, uint64, byte

The difference between integers with different digits is that they can save the size of the range of integers.

Signed types can store any integer, while unsigned types can only store natural numbers

The size of int and uint is related to the system. A 32-bit system represents int32 and uint32, and a 64-bit system means

Int64 and uint64

Byte is similar to uint8 in that it is generally used to store individual characters

Under the condition that the program runs correctly, try to use data types that take up less space.

Fmt.Printf ("% T", var_name) output variable type

Unsafe.Sizeof (var_name) View variable occupancy byte

Floating point type

The floating point type is the decimal type, which can store decimals. For example, 6.6May 12.34

A simple explanation of the form of floating-point numbers stored in the machine, floating-point numbers = symbolic bits + exponential bits + Mantissa digits

The Mantissa may be lost, resulting in a loss of accuracy. -123.0000901

Package main import "fmt" func main () {var num1 float32 =-123.0000901 var num2 float64 =-123.0000901 fmt.Println ("num1 =", num1, "num2 =", num2);}

Note: the accuracy of float64 is more accurate than that of float32: if we want to save a number with high precision, we should choose float64 floating-point storage to be divided into three parts: symbol bit + exponential bit + Mantissa bit. In the stored procedure, the precision will be lost. The floating-point type of golang defaults to the float64 type. Float64 should be used because it is more accurate than float32. 0.123 can be abbreviated to .123, and it also supports scientific counting that 512.34 is equivalent to 512.34.

Character

There is no special character type in Golang, so if you want to store a single character (letter), you usually use byte to save it. A string is a sequence of characters connected by a string of fixed-length characters. A string in Go is concatenated by a single byte, which means that a traditional string is made up of characters, whereas a string in Go is made up of bytes.

Characters can only be wrapped in single quotation marks, not double quotation marks, double quotation marks wrap the string when we directly output the type value, that is, we output the ASCII code value of the corresponding character. When we want to output the corresponding character, we need to use formatting to output Go language characters using UTF-8 coding, English letters account for one character, Chinese characters account for three characters in Go, the essence of the character is an integer, direct output Is the code value of the UTF-8 encoding for this character. You can directly assign a number to a variable, and then press% c to format the output. The unicode character type corresponding to that number is operable, equivalent to an integer, because they all have corresponding Unicode codes.

The package main import "fmt" func main () {/ / character can only be wrapped in single quotation marks, not in double quotation marks. The double quotation mark wraps the string var C1 byte ='a 'var c2 byte =' 0' / / when we directly output the type value, we output the ASCII code value of the corresponding character / /'a' = = > 97 fmt.Println (C1, "-", c2) / / if we want to output the corresponding character Need to use formatted output fmt.Printf ("c2 =% c c2 =% c", C1, c2)}

But if we save more than 255characters, such as storing Chinese characters, then the byte type cannot be saved.

Can be saved using the uint or int type when the

Discussion on the essence of character types

When the character type is stored in the computer, the corresponding code value (integer) of the character needs to be found and stored: character-> code value-- > binary-- > storage read: binary-- > code value-- > character-- > the corresponding relationship between reading character and code value is determined by the character coding table (which is prescribed). The coding of Go language is unified into UTF-8. It is very convenient and unified, and there is no trouble of coding garbled anymore.

Boolean type

Boolean type is also called bool type. Bool type data only allows value true or falsebool type to occupy 1 byte. Bool type is suitable for logical operations and is generally used for process control.

String

A string is a sequence of characters connected by a string of fixed-length characters. Go strings are concatenated by a single byte. Bytes of strings in Go language use UTF-8 encoding to identify Unicode text

Once the string is assigned, it cannot be modified:

Strings are immutable in Go.

Two identification forms of a string

Double quotation marks, which recognize escape characters

Var str = "abc\ nabc" / / Line wrapping on output

Backquotation marks are output in the native form of a string, including line breaks and special characters, which can prevent attacks and output source code.

Var str string = `output\ nabc` / / output as is, no escape

String concatenation mode "+"

Var str string = "hello" + "world" str + = "!"

When a line of string is too long, you need to use multiple lines of string, you can use the following processing

/ / correctly write str: = "hello" + "world!" Fmt.Println (str) / / misspelled str: = "hello" + "world!" Fmt.Println (str)

Pointer

Basic data types, variables store values, also known as value types get the address of the variable, use &, such as var num int, get the address of num: & num pointer type, pointer variable stores an address, the address points to the space where the value is stored, such as: var ptr * int = & num to get the value pointed to by the pointer type, use: *, for example, var ptr * int, use * ptr to get the value pointed to by ptr

Pointer details:

Value types, all of which have corresponding pointer types in the form of

* data type

For example, the pointer corresponding to int is * int,float64, and the pointer type is * float64, and so on. Value types include:

Basic data type

Array

And

Structure value type and reference type

Value type and reference type use characteristics: value type: variable directly stores value, memory usually allocates reference type in the stack: variable stores an address, the space corresponding to this address really stores data (value), memory is usually allocated on the heap, when no variable applies this address, the data space corresponding to this address becomes a garbage, which is reclaimed by GC. The distinction between value type and reference type in Golang

Value types: basic data types (int series, float series, bool, string), array and structure reference types: pointers, slice slices, map, pipe chan, interface, etc. are all reference types

Default values for basic data types

In Golang, all data types have a default value, which is retained when the programmer does not assign a value, and in Golang, the default value is also called zero. The default values for basic data types are as follows: data type default value integer 0 floating point 0 string "" Boolean type false

Package main import "fmt" func main () {var an int var b float32 var isTrue bool var str string / / the% v here means that fmt.Printf ("a =% v, b =% v, isTrue =% v, str =% v", a, b, isTrue, str) fmt.Println (")} is output according to the value of the variable.

Conversion of basic data types to each other

Unlike Java/C, Golang requires explicit conversion when assigning values between different types of variables. That is, data types in Golang cannot be converted automatically. Basic syntax: the expression var_type (var_name) converts the value v to type var_typevar_type: that is, the data type, such as int32, int64, float32, etc. Var_name: is the variable to be converted.

Var num int = 42 var float float64 = float64 (num) var ui uint8 = uint8 (float) fmt.Println (num, float, ui)

Matters needing attention

In Go, the conversion of data types can be from small range-> large range, or large range-> small range is

Data stored by variables

(that is, value), the data type of the variable itself has not changed! In the conversion, such as converting int64 to int8, there is no error at compile time, but the result of the conversion is handled as an overflow, which is different from what we want. Data conversion must be explicitly converted, not automatically converted

Package main import "fmt" func main () {var N1 int32 = 12 var N2 int64 var n3 int8 N2 = N1 + 20 / / int32-> int64 error N2 = N1 + 20 / / int32-> int8 error N2 = int64 (N1) + 20 / / correct N2 = int8 (N1) + 20 / / correct}

Define an integer of type int8 (var num int8 = 0). If you add 1 all the time, the value of this variable will be (0. 127-128-127. 0. 127) cycle over and over again without exceeding the maximum value of the type

Other basic types are converted to string types

In program development, we often need to convert numerical type to string type, or string type to numerical value.

Type.

Method 1:

Func Sprintf (format string, a... interface {}) string

Sprintf generates a formatted string based on the format parameter and returns it.

Package main import "fmt" func main () {var num1 int = 99 Var num2 float64 = 23.456 var isTrue bool = true var char byte ='A 'var str string str = fmt.Sprintf ("% d", num1) fmt.Printf ("str type is% T str =% Q\ n", str, str) str = fmt.Sprintf ("% f", num2) fmt.Printf ("str type is% T str =% Q\ n", str, str) str = fmt.Sprintf ("% t", isTrue) fmt.Printf ("str type is% T str =% Q\ n", str Str) str = fmt.Sprintf ("% d", char) fmt.Printf ("str type is% T str =% Q\ n", str, str)}

The output is

Str type is string str = "99" str type is string str = "23.456000" str type is string str = "true" str type is string str = "65"

Method 2: use functions of the strconv package

Package main import ("fmt"strconv") func main () {var num1 int = 99 Var num2 float64 = 23.456 var isTrue bool = true var str string str = strconv.FormatInt (int64 (num1), 10) str = strconv.Itoa (num1) fmt.Printf ("str type is% T str =% Q\ n", str, str) str = strconv.FormatFloat (num2, 'fallow, 10,64) fmt.Printf ("str type is% T str =% Q\ n", str, str) str = strconv.FormatBool (isTrue) fmt.Printf ("str type is% T str =% Q\ n", str Str)}

The output is

Str type is string str = "99" str type is string str = "23.4560000000" str type is string str = "true"

String type to other basic types

Method 1: use functions of the strconv package

Package main import ("fmt"strconv") func main () {var str string = "true" var str1 string = "123456" var str2 string = "123.456" var isTrue bool var num int64 var num2 float64 isTrue, _ = strconv.ParseBool (str) fmt.Printf ("str type is% T str =% v\ n", isTrue, isTrue) num, _ = strconv.ParseInt (str1, 10,64) fmt.Printf ("str type is% T str =% v\ n", num, num) num2 _ = strconv.ParseFloat (str2, 64) fmt.Printf ("str type is% T str =% v\ n", num2, num2)}

The data results are as follows:

Str type is bool str = true str type is int64 str = 123456 str type is float64 str = 123.456

Note: when converting string types to other basic data types, make sure that string types can be converted to valid data. For example, we can convert "123" to the number 123, but we can't convert "hello" to an integer. If we do this, Golang will directly convert it to 0, and the same is true for other types, float = > 0, bool= > false.

These are the Go language data types shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report