golang에서 매개 변수가 전달되는 방법의 함수로 매핑

당신은 때지도를 선언 할 때 :

m := make(map[int]int)

컴파일러는 호출합니다 runtime.makemap:

// makemap implements a Go map creation make(map[k]v, hint)
// If the compiler has determined that the map or the first bucket
// can be created on the stack, h and/or bucket may be non-nil.
// If h != nil, the map can be created directly in h.
// If bucket != nil, bucket can be used as the first bucket.
func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap

그래서 실제로는 HMAP에 대한 포인터를 반환합니다.

어떻게 확인합니까?

func main(){
    m := make(map[int]int)
    m[1] = 1
    fmt.Printf("原始map的内存地址是:%p\n", m)
    modify(m)
    fmt.Println("map值被修改了,新值为:", m)
}

func modify(m interface{}){
    fmt.Printf("函数里接收到map的内存地址是:%p\n", p)
    m := p.(map[int]int)
    m[1] = 2
}

출력 :

原始map的内存地址是:0xc00009e030
函数里接收到map的内存地址是:0xc00009e030
map值被修改了,新值为: map[1:2]

주요 기능으로는 m의 값이 저장되며, 포인터 변수이다 0xc00009e030.

함수를 수정하여, m은 포인터 변수이고, 상기 저장된 값은 다음 0xc00009e030.

설명 반환 함수 사이,지도 배달 주소, 포인터 변수,지도를 초기화합니다.

지도 및 채널은 유사하다.

왜 그것의 *지도 [키] 값 그래서,이보다 직관적 아닌가요?

이안 테일러는 A의 최근이 대답 golang 너트原话是:

우리가지도 지금 포인터로 작성되었고, 그래서 당신은 *지도 [INT] INT 쓴 부르는 초창기합니다. 우리는 아무도 쓴 없다는 것을 깨달았을 때 우리는 멀리 이동 map작성하지 않고 \*map.

시작은 * 맵 [키]의 값에 쓰고, 이후 모든 맵 포인터로서 사용되는 것으로, 그 때문에 생략 약칭 것을 의미한다.

추천

출처www.cnblogs.com/zhouj-happy/p/10962500.html