4. Go Language - value type and reference type

First, the value of the type

1. Definitions

Directly stored value of the variable, the memory is typically allocated in the stack; var i = 5->i-->5

2. Applications

int、float、bool、string、数组、struct

Second, the reference type

1. Definitions

Variable is stored an address, the final value of this address is stored. Memory is usually allocated on the heap. Recovered by GC.

ref r--->地址--->值

2. Applications

指针、slice、map、chan

Third, examples

package main

import "fmt"

func swap(a *int, b *int) {
    // 获取指针地址指向的值
    tmp := *a
    *a = *b
    *b = tmp
    return
}

func main() {
    one := 100
    two := 200
    // 获取指针地址
    swap(&one, &two)
    fmt.Println("one:%d", one)
    fmt.Println("two:%d", two)
}

Guess you like

Origin www.cnblogs.com/hq82/p/11072005.html