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

How to use data types in go language

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

Share

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

This article mainly introduces the relevant knowledge of how to use the data types in the go language, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the data types in the go language. Let's take a look.

Go language data types are: 1, Boolean; 2, numerical type (can be divided into integer and floating-point type); 3, string type; 4, pointer type; 5, array type; 6, structured type; 7, Channel type; 8, function type; 9, slice type; 10, interface type; 11, Map type.

The Go language is a statically typed programming language. In the Go programming language, data types are used to declare functions and variables. The emergence of data types is to divide the data into data with different memory sizes. When programming, you need to use big data to apply for large memory, so you can make full use of memory. When compiling, the compiler knows the type of each value so that the compiler knows how much memory to allocate for the value and what the allocated memory represents.

Basic data type description uint32-bit or 64-bit uint8 unsigned 8-bit integers (0 to 65535) uint16 unsigned 16-bit integers (0 to 65535) uint32 unsigned 32-bit integers (0 to 4294967295) uint64 unsigned 64-bit integers (0 to 18446744073709551615) int32-bits or 64-bit int8 signed 8-bit integers int16 signed 16-bit integers (- 32768 to 32767) int32 signed 32-bit integers (- 2147483648) To 2147483647) aliases for int64 signed 64-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 integers

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. 32-bit systems represent int32 and uint32, while 64-bit systems represent 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

1. A simple description of the storage form of floating-point numbers in the machine, floating-point numbers = symbolic bits + exponential bits + Mantissa digits.

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

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

Description: the accuracy of float64 is more accurate than that of float32.

Note: if we want to save a number with high precision, we should choose float64

3. Floating-point storage is divided into three parts: symbol bit + exponential bit + Mantissa bit. In the storage process, the precision will be lost.

4. The floating point type of golang is float64 by default.

5. In general, you should use float64 because it is more accurate than float32

6. 0.123 can be abbreviated to .123. 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 in double quotation marks, which are strings.

When we output the type value directly, we output the ASCII code value of the corresponding character.

When we want to output the corresponding characters, we need to use formatted output

The characters in Go language are encoded by UTF-8, with one character in English letters and three characters in Chinese characters.

In Go, the essence of a character is an integer, which, when output directly, is the code value of the UTF-8 code corresponding to the character.

You can directly assign a number to a variable, and then press% c to format the output, which will output the unicode character corresponding to that number.

Character types are operable, equivalent to an integer, because they all have corresponding Unicode codes

The package mainimport "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, and we can use the uint or int type to save it.

Discussion on the essence of character types

The character type is stored in the computer, and the corresponding code value (integer) of the character needs to be found.

Storage: character-- > code value-- > binary-- > storage

Read: binary-- > code value-- > character-- > read

The corresponding relationship between characters and code values is determined by the character coding table (which is prescribed).

The coding of the 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 types are also called bool types, and bool type data only allows values of true or false

Bool type occupies 1 byte

The 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

1. Once a string is assigned, it cannot be modified: in Go, a string is immutable.

2. Two identification forms of 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

3. String concatenation mode "+"

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

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

/ / correct writing str: = "hello" + "world!" fmt.Println (str) / / incorrect writing 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, to get the address of num: & num

Pointer type. The pointer variable stores an address, and the address points to the space where the value is stored, for example: var ptr * int = & num

Get the value pointed to by the pointer type, use: *, for example, var ptr * int, and use * ptr to get the value pointed to by ptr

Pointer details:

Value types all have corresponding pointer types in the form of * data types. For example, the pointer corresponding to int is * int,float64 and the pointer type corresponding to * float64 is * pointer, and so on.

Value types include: basic data types, arrays, and structure struct

Value type and reference type

Usage characteristics of value types and reference types:

Value type: variables store values directly, and memory is usually allocated in the stack

Reference type: the variable stores an address, and the space corresponding to this address actually stores the data (value). Memory is usually allocated on the heap. When no variable applies this address, the data space corresponding to the address becomes garbage and 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), arrays and structures

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:

The default value of the data type is integer 0 floating point 0 string "Boolean type falsepackage mainimport" fmt "func main () {var an int var b float32 var isTrue bool var str string / /% v here, which means that fmt.Printf is output according to the value of the variable (" a =% v, b =% v, isTrue =% v, str =% v ", a, b, isTrue Str) fmt.Println (")} this is the end of the article on" how to use data types in go " Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use data types in go language". If you want to learn more, 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

Development

Wechat

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

12
Report