There are 2 similar examples.

countryCapitalMap = make(map[string]string) countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "Rome" countryCapitalMap["Japan"] = "Tokyo" countryCapitalMap["India"] = "New Delhi" for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } 

Result:

Capital of France is Paris

Capital of Italy is Rome

Capital of Japan is Tokyo

Capital of India is New Delhi

Those. I get both the key value and the value by key.

Option 2

 x2 := make(map[int]string) x2[5] = "message" x2[1] = "Hello" for key := range x2 { fmt.Println("key[" + string(key) + "] is: " + x2[key]) if x2[key] == "message" { delete(x2, key) } } 

Result:

key [] is: message

key [] is: Hello

Why in this case I do not get the key value?

    1 answer 1

     package main import ( "fmt" "strconv" ) func main() { x2 := make(map[int64]string) x2[5] = "message" x2[1] = "Hello" for key := range x2 { fmt.Println("key[" + strconv.FormatInt(key, 10) + "] is: " + x2[key]) if x2[key] == "message" { delete(x2, key) } } } 

    To bring types there is a separate package, use it, also int64 (something like an extended type) you need to use it everywhere. All the best!

    • Thanks for the answer. I looked at the documentation for the golang.org/pkg/strconv/#example_FormatInt package, the question arose, what about the base? FormatInt (i int64, base int) - Sergei R
    • @SergeiR This is a number system. Binary, octal, decimal, etc. - Ainar-G