[Go language]slice and map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. 1 Slice Type
Slice is a reference type and is a dynamic pointer to an array slice.
Slice is a variable-length data structure that always points to the underlying array of arrays.
Src/pkg/runtime/runtime.h
struct! Slice
{ Must not move anything
byte* Array Actual data
UInt32 Len Number of elements
UInt32 Cap Allocated number of elements
};


1) Create slice
Dynamic array creation, similar to creating an array, but without specifying a fixed length
Var al []int Create slice
SL: = make ([]int,10)//Create a slice with 10 elements
sl:=[]int{1,2,3} To create a slice with initialization elements


2) Create the array first, and set up the slice based on the array slice
var arr =[10]{1,2,3,4,5. 6}
SL: = Arr[2:5] Create a slice with 3 elements


3) Slice has some simple operation
-The default starting position for ' slice ' is 0, ' ar[:n ' is equivalent to ' ar[0:n '
-The second sequence of ' slice ' is the length of the array by default, ' Ar[n:] ' equivalent to ' Ar[n:len (AR) '


3) The difference between arrays and slice
A. When declaring an array, the length of the array is indicated in square brackets or ..., when the slice is declared, empty in square brackets
B. As a function parameter, the array passes a copy of the array, and slice passes the pointer.


2 Map types
Map is a reference type, a key-value pair is stored in a dynamic way and stored dynamically.
Map is an unordered, variable-length data structure.
1) Create a map
var variable map[key Type] value type
var m map[string]int Create a map
M: = Make (Map[string]int)//Create Map
M: = map[string]int{"One": 1, "one": 2, "three": 3}//create a map with initialization elements
M: = map[string]int{
"One": 1,
"Both": 2,
"Three": 3, If you do not end with a}, you must change the line.
}




3 Array, slice,map operation
The next few are Golang predefined built-in functions that you can use without a guide.
1) Array, the length of the Slice,map, or the number of elements.
Len (SL) Gets the number of elements
Cap (SL) for maximum capacity


2) Add copy of Slice
Append (Target SL,... Element) adds one or more elements that return a slice (when the capacity is insufficient, the object pointed to by the pointer may be changed).
Copy (target SL1, source sl2[m:n]) copies the element as the m-n location of the source SL2. The 0-len location to copy to the target SL1.


3) Map assignment, deletion and verification
Map element Assignment
m["One"]=1


Delete a MAP element
Delete (M, "one")
m["one"] = 0,false


Verifies that the map element exists and returns True if there is OK, returning its value to Val
var val int
var OK bool
Val,ok = m["One"]


4) array, Slice,map loop traversal
Loop map, which returns the key and value of the map object
For key,val: = Range m{
Statement
}


4 Make and new
1) New
' New (t) allocates a memory space of type T, and points to a pointer to the *T type of the object. (The memory space is populated with the default value (0 value) when new)


2) make (s, length l, volume V) Create a data structure of type S. Returns an initialized type of s (equivalent to a pointer, make initializes the internal data structure, populates the appropriate values).
()
For example Slice, first create an array with an array length of "[Capacity V]" (the initialization value fills the default value), and then create a data structure for the slice fragment (pointing to the "length L" element before the array).




3) New and make differences
New (T) can be used to create a normal type, returning a pointer to the initialized T value.
Make (S) can only be used to create slice,map,channel. Returns an initialized s value (equivalent to a pointer).
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.