Ii. Slice and array differences, map considerations-go Language Notes

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

This time to talk about Array,slice,map. Although it has been said before, but the actual use of the use and understanding of these are not enough detailed

Now look at these types again.

Array type

Array is fixed-length arrays, which are the same as those in the C language, and must be determined before using the array length. But there are some differences compared to the arrays in C:

1 The array in Go is a value type, in other words, if you assign an array to another array, it is actually a copy of the entire array

2 If an array in Go is an argument to a function, then the actual argument passed is a copy of the array, not a pointer to the array. This and C should be separated. Therefore, if you pass an array as a function parameter in go, then the efficiency is definitely not passed the pointer high. Does this feel like a bit of a trap?

3 The length of the array is also part of the type, which means [10]int and [20]int are not the same.

The structure of the array is represented by a diagram:

Len represents the length of the array, and the following int stores the actual data

Slice type

See how effective Go is praised by slice:


Slice is the most commonly used type in the Go program to represent a sequence array. Why do you use it most often?

1 slice is variable-length

After defining a slice variable, you don't have to worry about its capacity, you can add data to the slice at any time.

Like what:

v:=[]string{}

V=append (V, "hello")

Incidentally, slice and array are easy to mix

v:=[2]string{"str1", "str2"}//This is an array

m:=[]string{"str1", "str2"}//This is slice

Writing on the meditation: array has length slice no length, array has length slice no length ...

2 Slice is a pointer rather than a value.

Pointer ratios can be much smaller, so it is more performance to pass slice as a function parameter than to pass an array as a function parameter.

Slice is a pointer to an array mechanism that has two basic functions Len and Cap.

Look at the following illustration:


Slice is a pointer to an array with a point, Len (the number of actual values in the array), CAP (the capacity of the array)

For example, above this slice, it points to the array is [3]int, where the first two have values, the third is empty

So

Len (slic) = 2

Cap (SLIC) = 3

The APPEND function is understood to add a value to the slice, if it does not reach the capacity (LEN<CAP) then add the value directly to the array, if it reaches the capacity (len = cap) then add a new element space, put the value inside

Map structure

The map structure is often used, and it is almost identical to the array () in PHP, which is a key-value hash structure. Key can be a type other than the Func type, Array,slice,map type.

It's also very simple to use

m:=map[string]string{}

m["Key1"] = "Val1"

The map structure and slice are the same, and are a pointer. When assigning a value, copy the pointer to the new variable

Map additions and deletions to the operation is this:

Package main import (    "FMT") func main () {    m: = map[string]string{"Key1": "Val1"}    fmt. Println (m)    m["Key2"] = "Val2"    fmt. Println (M)    p: = m["Key1"]    FMT. PRINTLN (p)    Delete (M, "Key1")    FMT. Println (M)}


The map must be created (not new) with make before it can be used; A map with nil value is empty and cannot be assigned a value. Func main () {m = make (Map[string]vertex) m["Bell Labs"] = vertex{40.68433, 74.39967,}fmt. The range format of the Println (m["Bell Labs"])}for loops can iterate over the slice or map. For I, V: = Range Pow {    fmt. Printf ("2**%d =%d\n", I, V)}




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.