Go Language Learning Notes: data types

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go language has been out for 5 years, only recently began to study, really ashamed.

If you want to use a word to describe the go language, then there is no more appropriate than high concurrency. LEVELDB is implemented using the go language.

Here's a look at the data type of the Go language

Boolean: bool, value true, FalseIntegral type: Int/uint 32-bit or 64-bit
8-bit integer: Int8/uint8BYTE type: Byte (unit8 alias)
16-bit integer: Int16/uint1632-bit integer: Int32 (Rune)/uint32
64-bit integer: Int64/uint64Floating point: Float32/float64 decimal point accurate to 7/15 bits
plural: complex64/complex128Sufficient 32-bit and 64-bit integer type to hold the pointer: uintptr
Other value types: array, struct, stringReference type: Slice, map, chan
interface Type: interfaceFunction Type: func. A function is a value that can be assigned to a variable.

# # # Individual variables are declared and assigned in the following three ways:

 1  
2
3
4
5
6
7
8
9
 //1   
var x int
x = 42

//2
var x int = 42
Span class= "line",
//3
x : = 42

It is important to note that the third type can be used only for local variables and not for global variables. The second way is to omit the type name, which is automatic inference by type.

How multiple variables are declared:

1
2
3
4
5
6
7
8
9
1
var (
int 1
int 2
int 3
)

2
var int 1 2 3

The first of these methods cannot be used for local variables. The second approach is useful, such as swapping the values of two variables

1
2
3
var 1
var 2
A, B = B, a

The Go language Constants support character, String, Boolean, and numeric types. The value of the constant is determined at compile time, and the definition format is basically the same as the variable. When you define a constant group, if you do not provide an initial value, the expression that will use the upstream is used, and of course the number of constants declared in both lines must be the same. It is worth mentioning that iota, the constant number of rows counter, starts at 0, and when a constant group is defined, each defined constant is automatically incremented. Iota resets to 0 for every const keyword encountered.

 1  
2
3
4
5
6
7
8
 const  ( 
A = 1
b
c = iota
d

Const E = iota
const F = iota

b does not provide an initial value, the right value of a is used, that is, b=1. C=iota, the number of rows counter, which is 2. D Use the right value of C, which is D=iota, then d=3. Each const in the iota is starting from 0, so e=0, f=0.

Array with pointers.

The array in the Go language is a value type, that is, if the array is an argument to a function, the actual passed argument is a copy of the array, not a pointer to the array. The Declaration and assignment of the array are as follows

1
2
3
4
5
6
 var A [4]int 
//now A = {0,0,0,0}
A = [4]int{1,2,3,4}
var B = [4]int{1,2,3,4}
var C = [...] int {1,2,3,4}
var D = [$]int{1,2}

D implements the assignment of an array-specified subscript. Pointers are basically the same as C-language pointers, but instead of "--", they take "." Directly. Selector to manipulate the pointer. & Access Address, * access value. Then a beloved question came. Distinguishes pointer arrays from array pointers. A pointer array is an array, an array of elements is a pointer, an array pointer, an array. Defined as follows

 1  
2
3
4
5
6
 //pointer array   
Span class= "line" >var x, y = 1 , 2
var a = [...] *int {&x, &y}
// Array pointer
var z = [...] int {x, y}
var b *[2 ] int = &z

It is important to note that the different lengths of the array types in the go language are not the same, so it is important to note that when declaring an array pointer.

Built-in type: Slice, map, channel

Slice are similar to tiles in Python. Can be understood as a pointer to an array. And this pointer also has two fields: length, capacity. The length is the current number of elements, the capacity is the maximum number of elements, similar to the vector size and capacity in the STL. Normally slice and arrays share a piece of storage space, and slice re-allocates the space when the slice operation makes the slice exceed the array size. You can also use make to allocate space for slice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
A: = [ten]int{1, 2, 3, 4, 5, 6, 7, 8, 9, ten}

Slice1: = a[5:ten]
FMT. Println (Slice1)
//[6,7,8,9,10]
a[5] = ten
FMT. Println (Slice1)
//[10,7,8,9,10]
Slice1 = Append(Slice1, +)
FMT. Println (Slice1)
//[10,7,8,9,10,100]
a[6] = One
FMT. Println (Slice1)
//[10,7,8,9,10,100]

Map associative array, two domain key, value. We know that the implementation of the map is based on the implementation of the tree, but also based on the implementation of the hash table, then the go language is how to achieve it? Have time to go to see the runtime source code. The map in the go language is implemented with a hash table, and the hash conflict is solved by means of the open chain. Write down the common usage of map below.

 1  
2
3
4
5
6
7
8
 Make (map[int]int)
mymap[ -ten-ten
mymap[555
mymap[

for Range Mymap {
Fmt. Println (k, v)
}

Channel is mainly used for goroutine, next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.