Go language arrays, Maps, slices operations

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

Arrays: Array

In the go language, an array of arrays is a set of ordered elements of a specific length, which can be any primitive type, such as shaping, string, or custom type, read (or modified) by index (position), index starting at 0, index of first element to 0, second index to 1, and so on

Array.go

Package Mainimport "FMT" Func Main () {    //Here we have created an array of length 5. The initial value of this set of arrays is zero-valued. The integral type is 0    var a [5]int    FMT. Println ("EMP:", a)    //Can be assigned by array[index] = value syntax    a[4] = the    fmt. Println ("Set:", a)    FMT. Println ("Get:", a[4])    //The built-in Len function returns the array length of    fmt. Println ("Len:", Len (a))    //The default initial value of the array is declared by this syntax    b: = [5]int{1, 2, 3, 4, 5}    FMT. Println ("DCL:", b)    //array type is one-dimensional, but you can create a multidimensional array structure by combining    var Twod [2][3]int for    I: = 0; I < 2; i++ {for        J: = 0; J < 3; J + + {            Twod[i][j] = i + j        }    }    fmt. Println ("2d:", Twod)}


[Root@mail work]# Go run array.goemp: [0 0 0 0 0]set: [0 0 0 0 100]GET:100LEN:5DCL: [1 2 3 4 5]2d: [  [0] 1 2] [1 2 3] ]

Maps: Key-value pairs
Maps is the associated data type in the Go language (sometimes referred to in other languages as a hash table [hashes] or dictionary [dicts])

Maps.go

Package Mainimport "FMT" Func Main () {    //use built-in make to build an empty map,make (map[key Type] value type)    m: = Make (Map[string]int)    Set key/value pairs using the classic Name[key] = Val syntax.    m["K1"] = 7    m["K2"] =    //print map will output all the key values inside the    FMT. PRINTLN ("Map:", m)    //Gets the value of a key    v1: = m["K1"]    fmt. The Println ("v1:", v1)    //len function Gets the number of key/value pairs in the map,    FMT. Println ("Len:", Len (m))    //Use the built-in delete function to remove a key/value pair from the map,    Delete (M, "K2")    FMT. PRINTLN ("Map:", m)    //optional second return value can indicate whether the value of this key is included in the map. Avoid ambiguity caused by a null value of 0 or "".    _, PRs: = m["K2"]    FMT. Println ("PRS:", PRS)    //You can also complete declaration and assignment in one line    N: = map[string]int{"foo": 1, "Bar": 2}    FMT. PRINTLN ("Map:", N)}

[Root@mail work]# Go run maps.gomap:map[k1:7 k2:13]v1:  7len:2map:map[k1:7]prs:falsemap:map[foo:1 Bar:2]

Slices: Slicing
Slices is a key data type in the go language, and it has a stronger access interface than an array (arrays).

Slices.go

Package Mainimport "FMT" Func Main () {//is different from array (arrays), the type of slices is consistent with the type of element contained (not the number of elements). Use the built-in make command to build a non-zero-length empty slice object. Here we create a string that contains 3 characters. (initialized to 0 value zero-valued) s: = Make ([]string, 3) fmt.    Println ("EMP:", s)//We can set and read as an array. S[0] = "a" s[1] = "B" s[2] = "C" FMT. Println ("set:", s) fmt.    Println ("Get:", s[2])//gets to the length that was set at that time. Fmt. Println ("Len:", Len (s))//compared to these basic operations, slices supports some more complex functions. One is the built-in append that adds one or more values to an existing slice object.    Note to re-assign a value to the returned Append object to get the most recent slice object that added the element. s = Append (S, "D") s = append (S, "E", "F") fmt. Println ("APD:", s)//slices can also be duplicated.    Here we copy S to C, consistent in length. c: = Make ([]string, Len (s)) copy (c, s) fmt. Println ("cpy:", c)//slices supports the "slice" operation, and the syntax is Slice[low:high] (that is, a segment value in the slice is intercepted).    The following code will fetch these characters: s[2], s[3], and s[4]. L: = S[2:5] FMT. Println ("SL1:", L)//From the beginning of interception to every 5 characters (except the value) L = s[:5] FMT. Println ("SL2:", L)//starts with the second (including) character intercept to the last L = s[2:] FMT.    Println ("SL3:", L)//We can put the declaration and assignment on one line. T: = []string{"G", "H", "I"} FMT. Println ("DCL:", T)//slices can be combined into multidimensional arrays.    A one-dimensional slices object can be unequal, which is not the same as a multidimensional array. Twod: = Make ([][]int, 3) for I: = 0; I < 3;            i++ {Innerlen: = i + 1 Twod[i] = make ([]int, Innerlen) for J: = 0; J < Innerlen; J + + { TWOD[I][J] = i + j}} fmt. Println ("2d:", Twod)}

[Root@mail work]# Go run slices.goemp: []set: [A B C]GET:CLEN:3APD: [a B  c d e f]cpy: [a b c d e F]SL1: [C D E]SL2: [a b C D E]SL3: [C D e F]DCL: [g H i]2d:  [[0] [1 2] [2 3 4]]




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.