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 structures commonly used in Go language

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the data structures commonly used in Go language are, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Learning goal

Master the use of common data types

The default value of Boolean type var v1 bool / / is falsev1 = true v2: = (1 = = 2) / / v2 will also be derived as bool type numeric type

Integer:

Int8, byte, int16, int, uint, uintptr, etc.

Value range of type length (bytes): int81-128 ~ 127uint8 (i.e. byte) 10 ~ 255int162-32 768 ~ 32 767uint1620 ~ 65 535int324-2147 483 648 ~ 2147 483 647uint3240 ~ 4 294 967 295int648-9 223 372 036 854 775 808 ~ 9 223 372 036 854 775 807uint6480 ~ 18 446 744 073 709 551 615int platform related platform related uintptr is 4 bytes under 32-bit platform and 8 bytes under 64-bit platform

Int and int32 are considered to be two different types in the Goto language, and the compiler will not automatically convert types for you.

Supported bit operators:

Example x y moves to the right > > 2 / / the result is 31x ^ y XOR 124 ^ 2 / / the result is 126x & y and 1242 / / the result is 0x | y or 124 | 2 / / the result is 126 ^ x and the result is  3

Floating point type:

Float32 、 float64 .

You can't directly use = = to judge whether two floating point numbers are equal.

You can judge the size of a floating-point type as follows:

Import "math" / / p is a user-defined comparison precision, such as 0.00001 func IsEqual (F1, f2, p float64) bool {return math.Fdim (F1, f2) < p}

Plural type:

Complex64 、 complex128 .

Complex numbers actually consist of two real numbers (represented by floating-point numbers in a computer), one representing the real part (real) and the other representing the imaginary part (imag). If you understand what the plural in mathematics is, then the plural in Go is very easy to understand.

Plural representation

Examples of plural representations are as follows:

Var value1 complex64 / / plural type consisting of 2 float32 value1 = 3.2 + 12ivalue2: = 3.2 + 12i / / value2 is complex128 type value3: = complex (3.2,12) / / value3 result is the same as value2

Real part and imaginary part

For a complex number z = complex (x, y), the real part of the complex number, namely x, can be obtained through the Go language built-in function real (z), and the imaginary part of the complex number, that is, y, can be obtained by imag (z).

The go language does not support implicit conversion (compared with implicit conversion of Java)

* * string: * * string.

Var str string / / declares a string variable with a default value of "slice and array"

Array declaration:

/ / Array definition var a [3] int / / declare and initialize the default value as 0a [0] = 1 / / assign the first number to 1b: = [3] int {1 int 2} / / declare and initialize c: = [2] [2] int {1 # 2}, {3, 43} / Multidimensional array initialization d: = [...] int {1pm 2pm 3pm 4pm 5} / automatic matching length

Array traversal:

Arr3: = [...] int {1, 3, 4, 5} for I: = 0; I < len (arr3); iShop + {t.Log (arr3 [I])} / / I is the index, without using index reference _ instead of for I, e: = range arr3 {t.Log (eMagol I)}

Interception of the array:

A [start index (inclusive), end index (not included)]

A: = [...] int {1Power2, 4 Len 5} a [1:2] / / result: 2a [1:3] / / result: 2, 3a [1: penny (a)] / / result: 2, 3, 4, 5a [1:] / result: 2, 3, 4, 5 a [: 3] / result: 1, 2, 2, 3, slice

Similar to the collection List in Java

Slice declaration:

/ / slice definition var s0 [] int / / initial length is 0s0 = append (s0Magne1) / / fill data s: = [] int {} S1: = [] int {1Power2} / * format: type,len,cap where len elements are initialized to default zeros, uninitialized elements are not accessible, that is, the index is 0 and the value of 1J 2 initializes to 0 and can be accessed 3 cap cannot be accessed by 2x growth (similar to the growth factor of list length in Java) * / S2: = make ([] int,3,5) /

Map

Map's statement:

Var variable name map [key type] value type

M: = map [string] int {"one": 1, "two": 2, "three": 3} M1: = map [string] int {} M1 ["one"] = 1 one / store data m2: = make (map [string] int, 100) / / set capacity / / determine whether the value exists if v, ok: = M1 ["one"]; ok {/ / value exists}

Note:

When key does not exist: the default value is output

When the corresponding value of key does not exist: the default value will also be returned

Traversal of map:

M1: = int {1: 1, 2: 4, 3: 9} / an is key b is value for a, b: = range M1 {t.Log (a, b)} / / k is key for k: = range M1 {t.Log (k)} what are the data structures commonly used in Go language Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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

Internet Technology

Wechat

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

12
Report