Go Language Basics Tutorial: Array,slice,map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. In this lesson, we'll talk about numbers, slices, and maps, or maybe you're going from other languages to go, and then in other languages you might not be comfortable with the number of go languages, because go languages split the numbers into Array,slice and maps, and. Next time let's study the difference and usage of these three. First, let's talk about the part of the array:
1. The length of the numbers is part of the group, and we look at the example below
a:=[1]int{} and b:=[2]int{}
Both A and B are numbers, but their length is different, and in go language they think they are two different types, since they are different types, they are not able to operate on a comparative or direct value. It is not difficult to understand that because go is a type of safe language, it is necessary to change the type of the value that crosses the type.
2. In go language, the number is a value type, not a reference type, and if you want to get the numbers, you need to do just that.
a:=[1]int{};p: =&a
Through the upper side of the language can get the number of the needle, and the value of the needle to the change P
The key name of the 3.array can only be used with integer or character type, and the following target starts from 0

The definition format for the array is:
var variable name = [length] Type {}

Then let's take a look at this code:
    a1:=[2]int{}//Voice a number that does not give the initial value, so the value of each element in the group is the initial values of int a2:=[2]int{1,2}//sound a group, and give the initial Value A3:=[2]int{1:2}//Voice a number to [1] The initial value of FMT.    PRINTLN (A1); Fmt.    PRINTLN (A2); Fmt.    PRINTLN (A3); P1:=&A1//Get the A1 of this number of fingers.    Println (p1); b:=[3]string{"AA", "BB", "CC"}//sound a string type of number, and give the initial value c:=[...] string{"AA", "BB", "CC"}//sound a number, do not set the length, with three points to tell go language itself to calculate the length of the FMT.    Println (b); Fmt.    Println (c); Fmt.              Println (B==C); Compare the two numbers to the same c[1]= "BB"//Modify the value of [1] fmt.              Println (B==C); The two numbers are the same as the FMT.            Println (Cap (c)); Get the number of capacity fmt.            Println (Len (c)); The key name to get the number of//array must be a number or a word d:=[...] Int{96:3, ' A ': 4}//Use the character as the key name, because each word Fu Shi is an int type ordinal, so its name is also the FMT of the int type.    Println (d); e:=[2][3]int{{1,1,1},{2,2,2}}//Voice a two-dimensional fmt.    Println (e); F:=[...] [3]int{{1,1,1},{2,2,2}}//sound a multi-dimensional group, which allows go language to calculate the FMT.    Println (f);             P:=new ([4]int)       This is another way of stating that you can use the New keyword, but this way of stating returns a pointer to the group FMT. PRINTLN (P);



Next, let's take a look at the slice slice, the section of the slice needs to know the following:
1. The slice itself is not a number, but his bottom layer points to the other numbers
2. Using slices to achieve the characteristics of the variable, we know that the length of the array is fixed
3. Slices are reference types, we know that array is a value type
4. Slice with make key, array with new
5. If multiple slices point to the same number, when a value of this group is changed, the values of all the slices follow this, which is the characteristic of the reference type.

The language format for creating the slices is as follows:
Slice name = make ([] group name, length, capacity)

Let's take a look at the usage of slices:
    S=make ([]int,3,5)            //This is the definition of the standard, defining the time when the slice is allocated    //fmt. Println (s);    A:=[...] int{1,2,3,4,5,6,7,8,9,0}    FMT. Println (a);    s:=[]int{}                    //define a sliced    fmt. Println (s);    S=a[5:]                        //allows the slice to point to number A, at the same time as its value    FMT. Println (s);    X:=a[4:9]    FMT. Println ("The value of X is:", x);    a[7]=99                        //Change some value of the group    FMT. Println (s);                Printing slices, the discovery value also changed the    FMT. Println ("The value of X is:", x);    This time the slice value has also been changed    s=a[:]                        //Get all the elements of    FMT. Println (s);



In the end, we'll look at how map is used, and map is different from the numbers or slices, so let's see what's different.
1.map use Key-value to save, the key value is no longer limited to the int type, can be the value of the string
2.map is much slower than numbers or slices, so if you can use numbers or slices, do not use map
3.map with slices is created using make, which is different from the new one
4.map Deleting an element with delete
5. Use range to iterate over the map or slice.

Next we'll look at the basic usage of map:
    var m1 = map[string]float64{"Bell Labs": 40.68433, "Google": 37.42202}    m1["new"]=1.01                    //New key value for    FMT. Println ("Three elements:", M1)    Delete (M1, "new")    FMT. Println ("Two elements:", M1)    FMT. PRINTLN (M1);                This key ["new"] already does not exist for    k, V: = Range M1 {            //iterate through the map        FMT. Printf ("m1[%s]=%f\n", K, V)    }    fmt. PRINTF ("M1 is%d \ n", Len (M1))    m: = Make (Map[string]int)        //Set map    m["K1"] = 7    m["K2"] = 13    FMT. PRINTLN ("Map:", m)


This can be seen array;slice;map each has its own different usage, in addition to the effectiveness of the difference, in the daily use of the use of the array and Slice,map, although the more alive is unfortunately not high performance.


Hope to download the article friends can leave an article out, Http://blog.csdn.net/sibang here grateful!

Just set up a QQ group, interesting talk about go language friends can join: 195112
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.