golang- notes 1


pointer:

Pointer is the address. Pointer variable is the variable to store addresses.

* P: dereference, an indirect reference.

Stack frame: means for providing memory space for operation of the function. Take on the memory stack.

When the function call, the stack frame is generated. The end of the function call to release the stack frame.

Memory stack frame: 1. The local variables. 2. parameter. (Local variable storage position between Form equivalents) 3. Memory Description Field Value

Pointer Caution:

null pointer: uninitialized pointer. var p * int * p -> err

Wild pointers: be an invalid address space initialization.

Formatted output:

% Q: Go display language format string. The default with the "" symbol

% V: Data show details

Variables are stored:

Variable on the left, representing the variable points of memory space. (write)

Variable to the right of the equal sign, on behalf of the data values ​​stored in the variable memory space. (read)

Transfer function pointer parameter (pass by reference).

Transmission address (reference): The parameter address value passed as arguments.

By value (according to the data): The value of an argument to copy parameter.

By-reference: A stack within the frame, modify the value of the variable B stack frame.

slice:

Why use a slice:

1. The capacity of the array is fixed and can not be automatically expanded.

2. The value of the transfer. As a function of a parameter array, the entire array is to copy the value of parameter.

When the Go language, we can almost all scenarios, using an array of slice replacement use.

Slice of nature:

Not a pointer array is a data structure, an array is used to operate the internal elements. Runtime / slice.go Slice type struct {
* P
len
slices using: CAP
}
array and slice defined differences:

When creating an array [] array of the specified length.

When you create a slice, [] is empty, or ...

Slice name [low: high: max]

low: initial indexing position

high: End index position len = high - low

Capacity: cap = max - low

Taken array initialization slice, the slice is not specified capacity, the capacity to follow the original array slice (slice).

s [: high: max]: from 0 to the high end. (Not included)

s [low:]: low from the beginning to the end

s [: high]: from 0 to the high end. Capacity to follow the original capacity. [Common]

Slice creation:

1. Automatic derivation types to create slices. slice: = [] int {1, 2, 4, 6}

2. slice: = make ([] int, length, volume)

3. slice: time = make ([] int, length) to create a slice, the capacity is not specified, the length == capacity. [Common]

Slice of function parameters - passed by reference. (Transfer address)

append: append element at the end of the slice

append (slice object, the element to be added)

When add elements to slice, slice capacity will grow automatically. When below 1024, an increase of a double way.

copy:

copy (target position in the slice, the slice source)

Copy process, a direct copy of the corresponding position.

map:

Dictionary, map key - value key: unique, unordered. You can not be a reference type data.

map can not use the cap ()

Create manner:
1. Map M1 var [int] String data can not be stored ---

2. m2: = map [int] string {} --- capable of storing data

3. m3: = make (map [int] string) --- Default len ​​= 0

4. m4 := make(map[int]string, 10)

initialization:

1. var m map [int] string = map [int] string {1: "aaa", 2: "bbb"} do not overlap each other to ensure that key.

2. m := map[int]string{ 1: "aaa", 2:"bbb"}

Assignment:

Assignment process, the new map if the key element is the same as the original key map elements -> cover (alternative)

The assignment process, if the key elements of the new map and the original map different elements key -> Add

Use the map:

traverse map:

for key值, value值 := range map {

}

for key值 := range map {

}

Analyzing the map key exists.

map [index] Operation: returns two values, the first value is a value table, if the value does not exist. nil

second key table type bool if present. There is true, false does not exist

Delete map:

delete () function: Reference 1: map element to be deleted Reference 2: key value

delete (map, key) to delete a non-existent key, not error.

map of function arguments and return values ​​passed by reference.




 

Guess you like

Origin www.cnblogs.com/landv/p/10958405.html