Go Language Basics (ii)-----Advanced data types

Source: Internet
Author: User

Go language-array type

An array is a container that can hold several types of elements of the same type. The size of the container (that is, the length of the array) is fixed and is reflected in the type literal of the array. For example, we declare an array type:

Type Mynumbers [3]int

Note: A type declaration statement consists of a keyword type , a type name, and a type literal.

The so-called type literal is used to represent a literal representation of a type (or notation method). In contrast, the literal representation of a value used to represent a type can be called a value literal, or simply a literal. For example, what has been mentioned before 3.7E-2 can be called floating-point number literals. The type literal consists of [3]int two parts. The first part is the length of the array wrapped in square brackets, that is [3] . This also means that the length of an array is part of the type of the array and is invariant. The second component of the type literal is int . It represents the type of element that the array can hold. In this case, the above-mentioned type declaration statement is actually [3]int an alias type declared for an array type. This allows us to use it as MyNumbers an array type [3]int .


When we represent a value of such an array type, we should write the type literal on the leftmost side, and enclose in curly braces the number of elements that the value contains. The elements are separated by commas (English half-width), i.e.:

[3]int{1, 2, 3}    

Now, let's assign this array literal to a numbers variable named:

var numbers = [3]int{1, 2, 3}

Note: This is a variable declaration statement. It assigns a value to the variable at the same time it declares the variable.

Another convenient way to do this is to omit a number in the type literal that represents its length, like this:

var numbers = [...] Int{1, 2, 3}

This frees us from having to count the number of elements to fill in that number.

Next, we can easily use an index expression to access any element of the variable's value, for example:

NUMBERS[0]//will get the first element numbers[1]//will get a second element numbers[2]//will get the third element

Note: An index expression consists of a string, an array, a slice, or a value of a dictionary type (or a variable or constant that represents such a value), and an index value wrapped in square brackets. Here, the valid range of index values is [0, 3]. That is, for an array, the index value cannot be less than 0 or greater than or equal to the length of the array value. Also note that the minimum valid value for an index value is always 0, not 1.

In contrast, if we want to modify an element value in an array value, we can use an assignment statement to achieve it directly. For example, if we want to modify numbers the second element in, we can:

NUMBERS[1] = 4

Although the length of the array has been reflected in its type literal, we still need to get it clearly in many cases, like this:

var length = len (numbers)

Note: Is the name of the built- len in function of the go language. This function is used to get the length of a string, array, slice, dictionary, or channel type value. We can use it directly in the Go language source file.

Finally, note that if we only declare a variable of an array type without assigning a value to it, then the value of the variable will be an array value of the specified length, where each element is a 0 value (or default value) of the element type. For example, if you have such a variable:

var numbers2 [5]int

Then it will be a value of

[5]int{0, 0, 0, 0, 0}

Package Main


Import "FMT"

Func Main () {

var numbers2 [5]int
Numbers2[0] = 2
NUMBERS2[3] = numbers2[0]-3
NUMBERS2[1] = numbers2[2] + 5
NUMBERS2[4] = Len (numbers2)
Sum: = 11
"= =" is used to determine the equality of two values
Fmt. Printf ("%v\n", (sum = = numbers2[0]+numbers2[1]+numbers2[2]+numbers2[3]+numbers2[4]))
}

Go language-slice type

Slices (Slice), like arrays, are containers that can hold several types of elements of the same type. Unlike an array, the length of its value cannot be determined by the slice type. Each slice value will use the array as its underlying data structure. We also refer to this array as the underlying array of slices.

The literal representation of the slice type is as follows:

Or

[]string    

As you can see, the only difference between them and the type literal of an array is that it does not contain information that represents its length. Therefore, slice values of different lengths are likely to belong to the same type. In contrast, array values of different lengths must belong to different types. The declaration of a slice type can be this:

Type Myslice []int

At this point, the type is MySlice []int an alias type for the slice type. In addition, the representation of the slice value is also very similar to the array value, such as:

[]int{1, 2, 3}  

The difference between this literal and the literal of the array (value) is only the leftmost type literal.

The same way we talked about manipulating array values in the previous section also applies to slice values. However, there is also a way to manipulate array values we didn't talk about. The name of this operation is called "slicing". The way to implement a slice operation is to slice an expression. Examples are as follows:

var numbers3 = [5]int{1, 2, 3, 4, 5}var Slice1 = Numbers3[1:4]

Notice that the second assignment statement is in the right part of the "=". A slice expression typically consists of a string, an array or a slice's value, and two positive integers that are wrapped in square brackets and delimited by the English colon ":". These two positive integers represent the lower index of the element and the upper-bound index of the element, respectively. In this case, the slice expression evaluates numbers3[1:4] to []int{2, 3, 4} . As can be seen, the evaluation result of the slice expression is equivalent to the new value formed by "cutting" from the manipulated object by the lower index of the element and the upper bound index. Note that the part that is "cut off" does not contain the element to which the upper-bound index points. In addition, the evaluation result of the slice expression is the slice type, and its element type is the same as the element type of the "slice" value. In fact, the slice1 underlying array of this slice value is exactly numbers3 the value.

In fact, we can also implement slice operations on a slice value. Operate in a manner similar to the above. Take a look at the following example:

Accordingly, slice2 the value is []int{3, 4} . Note that the length of the slice value that evaluates to the slice expression is always the difference between the upper-bound index of the element and the lower-bound index of the element.

In addition to the length, the slice value and the array value have another property--capacity. The capacity of an array value is always equal to its length. The capacity of the slice value is often different from its length. Please look.

, the capacity of a slice value is the absolute value of the index value of its first element value in its underlying array and the difference between the length of the array. To get the capacity of the value of an array, slice, or channel type, we can use the built-in functions cap , such as:

var capacity2 int = Cap (SLICE2)

Finally, be aware that the slice type belongs to the reference type. It has a value of 0, which is the nil null value. If we only declare a variable of a slice type without assigning a value to it, then the value of the variable will be nil . For example, if you have such a variable:

var slice3 []int

Then it will be a value of

nil

Package Main

Import "FMT"

Func Main () {
var numbers3 = [5]int{1, 2, 3, 4, 5}
Slice3: = Numbers3[2:len (NUMBERS3)]
Length: = 3
Capacity: = 3
Fmt. Printf ("%v,%v\n", (length = = Len (slice3)), (Capacity = = Cap (slice3)))
}

Go language-Dictionary type

The dictionary (MAP) type of the go language is actually an implementation of the hash table. A dictionary is used to store an unordered collection of key-element pairs (more commonly, key-value pairs). Note that each key in the same dictionary is unique. If we put a key-value pair in the dictionary that already has the same key, then the value associated with this key will be replaced by the new value.

The literal number of the dictionary type is as follows:

Map[k]t    

where "K" means the type of the key, and "T" represents the type of the element (or value). If we want to describe a type of dictionary with a key type int and a value type string , this should be the case:

Map[int]string    

Note that the key type of the dictionary must be comparable, otherwise it will cause an error. In other words, it cannot be a slice, dictionary, or function type.

The literal representation of a dictionary value is actually similar to the literal representation of an array and a slice. First, the leftmost is still the type literal, and the right side is enclosed by curly braces and has a comma-separated key-value pair. The keys and values for each key-value pair are separated by a colon. In the case of a dictionary type map[int]string , the literal of its value can be this:

Map[int]string{1: "A", 2: "B", 3: "C"}

We can assign this value to a variable:

MM: = map[int]string{1: "A", 2: "B", 3: "C"}    

Then use the index expression to take out the values in the dictionary, like this:

B: = mm[2]  

Notice here that we are no longer indexed in square brackets (in fact, the key-value pairs in the dictionary are not indexed), but rather the key that corresponds to the value we want to take out. In the example above, the b value of the variable must be a string "b" . Of course, you can also use an index expression to assign a value, such as this:

MM[2] = B + "2"   

This makes the mm value corresponding to the key in the dictionary 2 changed "b2" . Now let's go back to mm adding a key-value pair:

MM[4] = ""  

After that, the value corresponding to "and" is removed from it 4 5 :

D: = mm[4]e: = mm[5]  

At this point, the variable d and e the value will be how much? The answer is both "" , that is, the empty string. For a variable, the value of the d mm 4 "" Index expression must be evaluated because it is in the dictionary with the corresponding values mm[4] "" . That's for granted. But mm[5] why is the evaluation result also an empty string? The reason for this is that there is a provision in the go language that, for a dictionary value, if there is no key-value pair that the index expression wants to take out, then the null value of its value type (or the default value) is used as the evaluation result of the index expression. Because the null value of the string type is "" , mm[5] the evaluation result is "" .

Without knowing mm the exact value, what does it mean that we don't know mm[5] what the evaluation result means? Does it mean that 5 the corresponding value is an empty string? Or does it mean that there is no key- mm 5 value pair at all? This is not judged. To solve this problem, the go language provides us with another notation, namely:

E, OK: = Mm[5]

An index expression for a dictionary can have two evaluation results. The second evaluation result is bool of type. It is used to indicate whether a specified key-value pair exists in the dictionary value. In the example above, the variable ok must be false . Because a mm key value pair is not present in the 5 key.

The way to remove a key-value pair from a dictionary is simply to call the built-in function delete , just like this:

Delete (mm, 4)   

No matter mm If there is a 4 key value pair in the key, it delete will be executed "silently". We can generalize its behavior well by using the "then delete, then do nothing".

Finally, the same as the slice type, the dictionary type belongs to the reference type. It has a value of 0 nil .

Package Main

Import "FMT"

Func Main () {
mm2: = map[string]int{"Golang": $, "java": 1, "Python": 8}
mm2["python"] = 0
Mm2["Scala"] = 25
mm2["Erlang"] =50
Delete (mm2, Python)
Fmt. Printf ("%d,%d,%d \ n", mm2["Scala"], mm2["Erlang"], mm2["python"])
}

Go language-Channel type

Channel is a very unique data structure in the go language. It can be used to pass typed data between different goroutine, and is concurrency-safe. In contrast, none of the data types we described earlier are concurrency-safe. This requires special attention.

Goroutine (also known as go programs) can be seen as a vector that hosts code blocks that can be executed concurrently. They are dispatched by the run-time system of the go language and are executed concurrently by the operating system thread (also known as the kernel thread) to execute the code block. As for how to write such a block of code and how to drive such a code block execution, we first press the No table.

The method of representing the channel type is simple and consists of only two parts, as follows:

Chan T    

In this type literal, the left side is the keyword that represents the channel type chan , and the right side is a mutable part that represents the type of data that the channel type allows to pass (or the element type of the channel). The two sections need to be separated by a space.

Unlike other data types, we cannot represent a value for a channel type. Therefore, we cannot assign a value to a variable of the channel type by literal. We can only accomplish this by invoking the built-in function make . The make function can accept two parameters. The first parameter is the literal of the type that represents the value to be initialized (for example chan int ), and the second argument is the length of the value. For example, if we want to initialize a 5 channel value of length and element type int , we need to write this:

Make (chan int, 5)    

By the way, the make function can also be used to initialize the value of a slice type or dictionary type.


To be exact, the length of the channel value should be called its cached size. In other words, it represents the number of data that can be staged in a channel value. Note that the data in the channel value is FIFO first, i.e. the sooner the data that is put (or sent) into the channel value is taken out (or received).

Below, we declare a variable of a channel type and assign it a value:

CH1: = Make (Chan string, 5)   

In this way, we can use the receive operator <- to send data to the channel value. Of course, it can also be used to receive data from the channel value. For example, if we want to ch1 send a string to "value1" a channel, we should do this:

CH1 <-"Value1"  

On the other hand, if we want to ch1 receive the string from there, this is the case:

<-CH1    

At this point, we can directly assign the received string to a variable, such as:

Value: = <-ch1    

As with index expressions for dictionary values, a receive operation for a channel value can have a second result value. Take a look at the following example:

Value, OK: = <-ch1     


The purpose of this is also to eliminate ambiguities related to the value of 0. The value of the variable here ok is also of bool type. It represents the state of the channel value, which indicates that the channel value true is valid, and that false the channel value is invalid (or closed). The deeper reason is that if the channel value is closed before or during the receive operation, the receive operation ends immediately and returns a 0 value of the element type of the channel value. According to the first of the above, we have no way of judging what is the reason for receiving the zero value. However, with the second result value, this judgment is good to do.

When it comes to closing the channel value, we can do this by invoking the built-in function close , like this:

Close (CH1)   

Note that repeated shutdowns of the channel values can cause a run-time panic. This can cause the program to crash. So be sure to avoid this happening. Also, if the channel value is valid, the send operation against it is blocked when the channel value is full (where the number of cached data is equal to its length). Sending data to a channel value that has been closed can cause a run-time panic. On the other hand, a receive operation against a valid channel value is blocked when it is empty (where no data is cached). In addition, there are several rules related to the send and receive operations of the channel. But here we have to remember these three lines.

Finally, as with the slice and dictionary type, the channel type belongs to the reference type. It has a value of 0 nil .

Package Main

Import "FMT"

Func Main () {
CH2: = Make (chan string, 1)
Here's the legendary way to execute code blocks concurrently by enabling a goroutine.
The keyword Go is followed by a block of code that needs to be executed concurrently, which is represented by an anonymous function.
For the Go keyword and function writing methods, we will do a special introduction later.
Here, we just need to know that in curly braces is the code that will be executed concurrently.
Go func () {
CH2 <-"has reached! "
}()
var value string = "Data"
Value = value + <-CH2
Fmt. Println (value)
}




Go Language Basics (ii)-----Advanced data types

Related Article

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.