The first knowledge of Golang, Slice,map learning

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

defined by: the Var arr [n]type,n represents the length of the array, and type represents the types of the stored elements. Once defined, you can use arr[0] = 42, which specifies the subscript to read and assign the array.

Note: The length in Go is also part of the type, so [3]int and [5]int are different types.

The array in go can not change the length, the assignment between the array is the value of the copy assignment, that is, when an array as a parameter to the function, the incoming is actually a backup of the array. Arr: = [10]int{1,2,5:6},da: = [2][4]int{[4]int{1,2,3,4}, [4]int{5,6,7,8}}, if the inner element is the same as the outer one, then the declaration can be simplified, directly ignoring the inner type ea: = [2][4 ]int{{1,2,3,4},{5,6,7,8}}, if you do not know the number of arrays, but follow the initialization of the given value, use arr: = [...] int{1,2,3}, with three "." Point number to specify.


Slice slices:

Slice is the "dynamic array", the value of the time he is also copy assignment, but he copied the memory address, is to pass the pointer. Slice always points to an underlying Array,slice declaration as well as an array, but does not require a length.

Disclaimer: Var ys []int,slice can be declared again from an array or an already existing slice, slice through array[i:j], where I is the starting position of the array, J is the end position, the point does not include J, its length is j-i, if I is not specified , the default starts at the beginning and does not specify J until the last digit of the array. Note: The slice reference is a memory address, so when you change the value of the element in it, all other points to the slice change the value. Len () Gets the length of the slice, the cap () gets the maximum capacity of the slice, and the capacity of slice is the starting position of the extracted array to the end of the array, i.e.:

arr: = [10]int{1,2,3,4,5,6,7,8,9,10} s: = Arr[2:5]

the Len (s) is 3, and the CAP (s) is 8. Append (s, appended elements), if there are no more than slice cap values, the original slice reference is returned, affecting other slice that refer to the same array, if the CAP value is exceeded, a new slice is created and a reference to the new slice is returned. This state will be divorced from the original slice reference relationship.

The copy () function copies elements from the src of the source slice to the target DST and returns the number of copied elements, such as:


SL1: =[]int{ten,2,3}
SL2: =make([]int,Ten )
Copy (sl2[5:8],sl1[:2])

The result is 0,0,0,0,0,10,2,0,0,0,0

Map:
definition: Map[keytype]valuetype, to declare the types of keys and values, such as: Map[string]int,map read and set the same as slice, through key operation, declare can use article: = map[ string]string{"Tech": "The Google ...", "Music": "or fly to stay angry ..."}, or use make to create a memory space, and then use the subscript form to assign values, such as
article: = Make (map[string]string),
article["tech"] = "the Google ..."
article["Music" = "Who knows where we should go ..."

Note: The map is unordered and the map value will be different each time you print it.

to determine if the map has a corresponding subscript can be used: Golang, OK: = language["Go"], through Map[key] can return two values, a return to the corresponding subscript value, a if the specified subscript in the map does not exist return false, The presence returns TRUE.

Delete the element that specifies the subscript, using delete (map, "key").


In the go statement, only these three types are copy memory address, Slice,map,channel, the other is copy assignment, Len () function can also be used to get the number of keys in the map. Make can allocate memory for these three types, and it can only be used on these three types, making initializes the internal data structure, populates the appropriate values, returns the initialized non-0 values, and new is used for various types of memory allocations, new (type) A 0 value is allocated for the type of memory space that is populated and returns its address. That is, it returns a value (pointer) of a memory address of type *type.


"0 value", not NULL, not all 0, for integers,floats "0 value" is 0, for Boolean value is False, for string "0 value" is an empty string. pointers, functions, interfaces, slices, maps, channels's "0 value" is nil. The value "0" cannot be judged as Boolean false, such as:

var a int

if a {

FMT. Println ("true")

} else {

FMT. Println ("false")

}

this is an error.




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.