Roll up sleeves refueling dry Golang into the pit series

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

It is also a reminder that there are jokes inside, not all technology. Directed at the technology, walk not to send. No copyright, but can send me e-mail (ztao8607@gmail.com)

In my hair children, finally the last one to be married. To tell the truth, it's hard to be true. Blind Date seven or eight times, the woman age to 32, down to 23. The span of the large, wide range of my hair in the small boundary is quite rare. Ask yourself, what is wrong with the programmer? Why it's so hard to get married with a girlfriend. Is the coder to understand the amorous feelings? Or is the island sister blinded by the heart? If you speak dumb, introverted. That is probably the biggest misunderstanding of the yards, in their a reluctant to talk under the mask hidden a surging heart, always in the burst of inspiration. But as far as this inspiration is used on girls, it is not yet known. Also seen the tongue mouth, eloquent cock silk, but this state often appear in the dispute with people technical issues, if the opposite station is a cute sister, I am afraid that more hello world will return back to the belly inside. So, I can not find the other half of the hair, there is a reason for it. Fortunately, after a full range of efforts, finally found a learned to pry open the lippy tongue of the sister. So, to wish two happy knot, bald to the old, roll up the sleeves refueling dry!

Next to the book, continue to talk about the data types that are common in Golang.

In the previous article, I talked about string. This section looks at arrays array. If you still have an impression, there must be a < data structure > course on top of the college class. This course should be the cornerstone of the computer Science course, learn the course, even if you do not write code will not delay you find a good job. If you learn the algorithm well, even if you do not write code, do not delay you to bat internship.

Arrays are a common data structure. There is no way to get an array from a data structure point of view, because I think I am not proficient in this, not speaking well. Interested, suggest to look at the university data structure of the textbook, read that this is enough.

Golang arrays are no different from other languages in terms of data structures. are chained to store data and store the same type of data. The array subscript is also counted from 0, accessed sequentially through []. For example:

intList[0] intList[1] ... intList[99]

The most common way to declare an array is the following:

var arrayname[SIZE] type

This is the most commonly used single-dimension array (an array of only one dimension), which corresponds to a multidimensional array, such as a two-dimensional array. Each item of a two-dimensional array is another array, so it can be understood as an array of stored arrays.

When declaring a variable with the above statement, size must be an integer constant greater than 0, and the type can be any valid data type (including built-in base types or custom data types).

For example, we declare an array of integers of length 10:

var intList[10] int

In the < prepare charge > section, you can see that outside the variable declaration, Golang automatically completes initialization if it is a variable of the base data type. What about the array? Golang will automatically complete initialization. You can try it yourself by writing a piece of code yourself. If you do not want to write, take the following code directly:

package mainimport (    "fmt")func main() {    var intList [10]int    fmt.Println(intList[1])    fmt.Println(intList[9])}

Arbitrary access to two elements, with a result of 0. Change to another type to see the results. The actual results show that, if it is a basic data type, Golang also initializes the array elements. In the example above, all of the elements are initialized to 0. What if I want to initialize the rest of the data? Easy, use the following syntax:

var name = [SIZE]type{ value, value .... }

From the syntax above, it is a two-in-one. This completes the initialization of the data by declaring the array first, and then assigning each element in the set to a value. such as the following:

var intList = [5]int{1,2,3,4,5}

Run the code and see the results:

package mainimport (    "fmt")func main() {    var intList = [5]int{1,2,3,4,5}    fmt.Println(intList[1])    fmt.Println(intList[4])}

This method is commonly used in the initialization, but it is less common, but the interview is easy to mention (under normal circumstances, the use of less than 10% probability).

隐式长度var intList = []int{1,3,4,5,6}

Do not declare length beforehand, rely on the length of the initial value to determine the length of the array, nothing to say, tricks.

than this just used to be:

var intList []intintList = append(intList,10)

But at this point the intlist has become a slice type. Slices and arrays are different, and in the next section we'll talk about the slice type, which is just a spoiler.

Once the array has been initialized, it can be used directly in subsequent programs. The use of the method is to take the subscript in turn out of the assignment or value operation. For example:

// 直接拷贝个源代码,用来说明问题package mainimport "fmt"func main() {   var n [10]int   var i,j int   for i = 0; i < 10; i++ {      n[i] = i + 100    }   for j = 0; j < 10; j++ {      fmt.Printf("Element[%d] = %d\n", j, n[j] )   }}

Golang the most basic array usage is these, in the beginning, said that the most commonly used is a one-dimensional array, multidimensional array use is not too much (especially more than two-dimensional array, with very little), so use a bit of space for the use of multidimensional arrays.

The first is the declaration of multidimensional arrays, it is not difficult to look at the responsibility:

var name [SIZE1][SIZE2][SIZE3]....type

Since it is an array, it is necessary to follow the specification of the array, whether it is a multidimensional array or an single-dimension array, where the elements must be of the same data type, so you can see that there is only one type at the end.

And the number of the above number of size represents the dimension, the above three size represents three-dimensional (for example, often said the length of the width, if you want to understand the three-dimensional, whatever you, is right). If only [Size1][size2] is said to be two-dimensional.

The initialization of multidimensional arrays is the same as one dimension (we can actually dimension the multidimensional arrays into one dimension), see below:

var a = [3][4]int{     {0, 1, 2, 3} ,      {4, 5, 6, 7} ,      {8, 9, 10, 11}   }

A is a two-dimensional array, with three elements above the first dimension, each of which is an array (the array is a single-dimension array that holds four elements). So the initialization of this array is the initialization of multiple arrays at the same time.

Multidimensional array access, still follows the rules of subscript access. For example, you need to remove 8 from the above array, which is a[2][0]. Because the array of 8 is the third element of the A array, first remove a[2], and this a[2] is an array, 8 is the first element of the array, so a[2][0] is 8.

Practice makes perfect, the only hand-cooked er. So if you don't understand, write a few more pieces of code to see the effect.

Finally ask a small question, let you answer.

Question: [5]int and []int is not the same data type

Answer: XXXX

Or that sentence, write a code to verify.

package mainimport "fmt"func main() {    var s []int    s = append(s, 10)    printArray(s)}func printArray(s []int) {    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)}

If the PrintArray parameter is replaced by []int] [5]int, see what effect. Yes, an error. will prompt you for type inconsistency. This is where it's easy to confuse when you start writing Golang code, [5]int is an array type, and []int is the slice type.] This will result in an error.

In actual programming experience, the array usage rate is not as high as the slice. Why is this? Don't order a star, subscribe to the next section.

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.