Go language Basics: Array, slice, make and new operations, map

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

Array

Array declarations and assignments

The go Array declaration is similar to the C language, except that the array type is placed after the variable name "This is the same as the declaration of the variable".
The format of the array definition:

//一维数组var//需要注意的是:'[n]'可以写成'[...]',go会自动根据元素个数来计算长度,//我猜应该是编译的时候计算的吧
//二维数组var 数组名[2][n]int{[n]数组类型{..},[n]数组类型{}}

Declaration and assignment of an array:

//one-dimensional arrayvarAR = [5]byte{' A ',' B ',' C ',' d ',' E '}//can alsoAR: = [5]byte{' A ',' B ',' C ',' d ',' E '}//Note that this can only be defined inside the function, which is the local variable//Two-D arraysvararr = [2][4]int{ [4]int{1,2,3}, [4]int{4,5,6} }//You can omit the [4]int] in {}vararr = [2][4]int{ {1,2,3}, {4,5,6} }

Array capacity and length

Go, the function cap() and the len() output array of the capacity and length, it is not difficult to find that the capacity of the array is the same as the length.

Slice

The go array uses the idea of C, then slice is the Java idea. The essence of slice in memory is actually an array, which is represented as contiguous blocks of memory. But it is different from the array, once the array is defined, its size is fixed, and the size of the slice is changeable, but not the real dynamic array in Java, but the reference of the array, which is a reference type.
the declaration of Slice is the same as the declaration of an array, except that it does not need length.
Slice's statement:

var fslice []int//注意这里没有长度,所以不要把它认为是数组哦//同样也可以slice := []int{1,2,3}

Declaration and assignment of sliced:

var ar = []byte {'a''b''c''d''e'}//也可以ar := []byte {'a''b''c''d''e'}//注意这个只能是函数内部定义,也就是局部变量

We can also declare it from an array or an already existing slice. Slice by array[i:j], where I is the beginning of the array, J is the end position, but does not contain array[j], and its length is j-i

//Declare an arrayvarArray_ori = [ One]byte{' A ',' B ',' C ',' d ',' E ',' F ',' G ',' h ',' I ',' J ',' K '}//Declaration of two slicevarSlice_a,slice_b[]byte//slice_a points to array Array_ori, 2nd element ' array_ori[2-1] ' start, 5th Element ' array_ori[5-1] ' endsSlice_a = array_ori[2:5]//slice1 points to array slice_a, 3rd element ' slice_a[3-1] ' start, 5th Element ' slice_a[5-1] ' endsSlice_b = slice_a[3:5]//What do you want to output first?

Its data structure is as follows:

According to, it is not difficult to derive the content output of Array_ori, Slice_a, Slice_b as follows:

Array_ori:[a b c d e f g h i j k]slice_a:[c d e]slice_b:[f g]

is not a familiar feeling of slice, yes, feel with the C pointer is very similar to the beginning of learning when you feel the idea of a dynamic array of Java, this will have C pointers to the idea, the baby is very messy ah.

Several built-in functions of slice

    • cap (): The maximum capacity of the Slice, and the cap () values for the slice_a and Slice_b in the example above are 8 and 5, respectively. Officially due to the slice_a capacity of 8, so the assignment to Slice_b is valid
    • Len (): Length of Slice, described above, slice_a and Slice_b len () values are 3 and 2, respectively
    • append (): Appends one or more elements to the slice and returns a slice of the same type as slice
    • Copy (): function copy copies elements from the src of the source slice to the target DST, and returns the number of copied elements

Make and new operations

Both make and new can be used for memory allocation.

    • New is used for various types of memory allocations, essentially the same as Java's new functionality. New (T) returns a pointer to the 0 value of the newly assigned type T.
    • Make can only be used to allocate memory for slice, map, and channel types, and it returns a T type with an initial value (not 0) instead of a pointer T.

Map

Map is the same as the Pythoon dictionary concept, messy, and began to have the idea of Python. The point is I don't know python, but it's okay, I'll learn it later.

Format of Map:

map[keyType]valueType

The Declaration of Map:

    • Use the Map keyword:var map_variable map[keyType]valueType
    • Using the Make function, return a map to map_variable:map_variable = make(map[keyType]valueType)

Declaration and assignment of map

varmap[stringint// 另一种 map 的声明方式make(map[string]int)numbers["one"] = 1//赋值 key是“one”,值是1numbers["ten"] = 10//赋值numbers["three"] = 3fmt.Println("第三个数字是: ", numbers["three"// 读取数据打印出来如:第三个数字是: 3

Map considerations:

    The
    • map is unordered, each time the map is printed differently, it cannot be obtained through index, and the length of the
    • map must be obtained through key is not fixed, that is, as slice, is also a reference type
    • The built-in Len function also applies to the map, which returns the number of keys owned by the map
    • The value of the map can be easily modified by numbers["One"]=11 can easily change the key to one's dictionary value to one-to-one
    • map can be initialized by Key:val, and the map has a built-in way to determine if a key exists, such as: map_variable: = map[string]string{"A": "1", "B": "2", "C": "3"}
    • Delete the element of map via Delete, function prototype Delete (map_variable, key)
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.