Slices shards in the 009_go language

Source: Internet
Author: User
Tags apd

Code Demo
 PackageMainImport "FMT"funcMain () {s: = Make([]string,3) FMT. Println ("EMP:", s) s[0] ="a"s[1] ="B"s[2] ="C"Fmt. Println ("Set:", s) fmt. Println ("Get:", s[2]) FMT. Println ("Len:",Len(s)) s =Append(S,"D") s =Append(S,"E","F") FMT. Println ("APD:", s) c: = Make([]string,Len(s))Copy(c, s) fmt. Println ("cpy:", c) L: = s[2:5] FMT. Println ("SL1:", l) L = s[:5] FMT. Println ("SL2:", l) L = s[2:] FMT. Println ("SL3:", l) T: = []string{"G","H","I"} FMT. Println ("DCL:", t) Twod: = Make([][]int,3) forI: =0; I <3; i++ {Innerlen: = i +1Twod[i] = Make([]int, Innerlen) forJ: =0; J < Innerlen; J + + {Twod[i][j] = i + j}} fmt. Println ("2d:", Twod)}
Code Run Results

EMP: []
Set: [a B c]
Get:c
Len:3
APD: [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]]

Code interpretation:
    • Sharding is a key data type in the go language and can provide a more useful sequence interface than an array
    • Unlike arrays, the types of shards are defined only by the elements they contain. Create an empty shard with built-in method make must be of length
    • Elements of set and get shards can be like arrays
    • Use the method Len to return the length of the Shard
    • In the basic operation of a Shard, the Shard supports an extra array of methods. One is built-in method append, you can append elements to the Shard
    • Shards can also be copied
    • Shard Support slicing operation, syntax Slice[low:high]
    • For a shard, we can declare it and initialize the element content, and do it in one line
    • Shards can also have multidimensional structures, and inner shards are mutable, unlike multidimensional arrays

Slices shards in the 009_go language

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.