How to copy a map to another map in Go

1 Answer

0 votes
package main
 
import (
"fmt"
)

func main() {   
    mp := map[string]int {"go":645,
                          "php":989,
                          "c++":821,
    }
    mpcopy := map[string]int{}        
     
    for index, element := range mp {        
         mpcopy[index] = element
    }
     
    for index, element := range mpcopy{
        fmt.Println(index, "-", element)  
    }
}




/*
run:

php - 989
c++ - 821
go - 645

*/

 



answered Aug 23, 2020 by avibootz
...