Learning Golang Language (6)

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

Go has pointers. The pointer holds the memory address of the variable.

Type *t is a pointer to the value of type T. Its 0 value is ' nil '.

var p *int

The & symbol generates a pointer to its action object.

I: = 42
p = &i

The * symbol indicates the underlying value that the pointer points to.

Fmt. PRINTLN (*P)//Read I via pointer p
*p = 21//via pointer p set I

This is often referred to as an "indirect reference" or a "non-direct reference".

Unlike C, Go has no pointer operation.


2. Structural Body

A struct (' struct ') is a collection of fields.

(The meaning of type is consistent with its literal meaning.) )


3. Structure Field

The struct field is accessed using the dot number.


4, the structure of the pointer

The struct field can be accessed through a struct-body pointer.

Indirect access through pointers is transparent.


5. Structural Grammar

struct grammar represents a new allocation of a struct by the value of the struct field as a list.

Use the Name: syntax to list only a subset of fields. (The Order of field names is irrelevant.) )

Special prefix & Returns a pointer to the struct body.


6. Arrays

Type [n]t is an array with n values of type T.

An expression
var a [10]int

The definition variable A is an array with 10 integers.

The length of the array is part of its type, so the array cannot change size. This may seem like a constraint, but don't worry, go provides a more convenient way to use arrays.


7, Slice

A slice points to the value of a sequence and contains the length information.

[]t is a slice with an element type of T.


8, the slice slice

Slice can be re-sliced, creating a new slice value that points to the same array.

An expression
S[lo:hi]

Represents the slice element from lo to hi-1, with both ends. So
S[lo:lo]

is empty, and
S[LO:LO+1]

has an element.


9. Construction Slice

Slice is created by function make. This assigns a 0-length array and returns a slice that points to the array:
A: = Make ([]int, 5)//Len (a) =5

In order to specify the capacity, the third parameter can be passed to ' make ':
B: = make ([]int, 0, 5)//Len (b) =0, Cap (b) =5
b = B[:cap (b)]//Len (b) =5, Cap (b) =5
b = b[1:]//len (b) =4, Cap (b) =4


10. Nil Slice

The 0 value of slice is ' nil '.

The length and volume of a nil slice is 0.


11. Adding elements to slice

Adding elements to slice is a common operation, so Go provides a built-in function of ' append '. The documentation for the built-in functions is described in detail in append.

Func append (s []t, vs ... T) []t

The first parameter s of append is an array of type T, and the remaining values of type T are added to slice.

The result of the append is a slice that contains all the elements of the original slice plus the newly added elements.

If the underlying array of S is too small to hold all the values, a larger array is allocated. The returned slice will point to the newly allocated array.


12. Range

The range format for the for loop can iterate over the slice or map.


13. Range (cont.)

You can omit the ordinal and the value by assigning a value to _.

If you only need the index value, remove the ", Value" section.


14. Map

Map map key to value.

The map must be created using make instead of new before it is used, and the map with nil value is empty and cannot be assigned a value.


15, map of the grammar

Map grammars are similar to struct grammars, but must have key names.


16. Map Grammar (cont.)

If the top-level type has only type names, you can omit the key names in the elements of the grammar.


17. Modify Map

Insert or modify an element in map m:
M[key] = Elem

Get elements:
Elem = M[key]

To delete an element:
Delete (m, key)

Detecting a key exists by double-assigning values:
Elem, OK = M[key]

If key is in M, ' OK ' is true. Otherwise, OK is ' false ', and Elem is a 0 value of the element type of map.

Similarly, when a nonexistent key is read from a map, the result is a 0 value of the element type of map.


18. Function Value

The function is also a value.


19, the closure of the function

The Go function can be a closed packet. A closure is a function value that comes from a variable reference outside the body of the function. The function can access and assign the value of the reference, in other words, the function is "bound" to this variable.

For example, the function adder returns a closure. Each closure is bound to its own sum variable.

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.