Go a daily question (2)

 

Reference the new copy of the elements, rather than a direct reference element

 

main Package 

Import ( 
    " FMT " 
) 

FUNC main () { 
    // fmt.Println ( "the Hello World") 
    
    Slice: = [] int { 0 , . 1 , 2 , . 3 } 
    m: = the make (Map [ int ] * int ) 
    
    for K, V: = Range Slice { 
        
        // Wrong Sample refer to the same variable address
         // m [K] V = & 
        
        // correct Sample, the new variable address 
        Val: = V 
        m [K] = & Val 
    } 
    
    for K, v: = range m {
        fmt.Println(k, "->", *v)
    }
}

 

Guess you like

Origin www.cnblogs.com/dzone/p/12103639.html