Go Learning (memo)

array和slice

In Go, the array is the value. An array of arrays are given another copy all of its elements. If an array passed to a function, it will receive a copy of the array rather than a pointer. Size of the array is part of its type. Type [10] int and [20] int is different. An array is a fixed allocation can not be changed. When the detailed planning of memory layout, the array is very useful, and sometimes also to avoid excessive memory allocation, but they are mainly used as a slice member. Slice holds a reference to the underlying array, if you will be given another slice of a slice, they refer to the same array. If a function is passed as a parameter to a slice, it is also visible to amend sections of the elements of the caller, this can be understood as a pointer passed the bottom of the array.
In simple terms, it is the value of the array, similar to the slice pointer array. Other copy follows the value .

Structure Functions

go function .go not belonging to the function of the structure has a feature can be attributed to the function of any type (or any type of pointer), the function corresponding to the ownership paid to someone, a person can directly call the function. The function the ownership to the structure pointer, the structure you can call this function while using this structure (the name of this structure as pointers) inside the function. in addition,
if the home is a function of the structure (rather than pointer), the structure can still be used, but the structure modification will not affect the function is external copy (transfer, but a copy of the pointer dereferencing would point to the source object)

package main

import "fmt"

type Handle int64
func (h Handle) Show1(i int64) int64{
    h = 1
    return i + int64(h)
}
func (h *Handle) Show2(i int64) int64{
    *h = 2
    return i + int64(*h)
}

func main() {
    var hand Handle = 0
    fmt.Println(hand.Show1(100))
    fmt.Println(hand)

    fmt.Println(hand.Show2(100))
    fmt.Println(hand)
}

101
0
102
2
Released six original articles · won praise 0 · Views 212

Guess you like

Origin blog.csdn.net/jingshuipengpeng/article/details/105121549