Go 如何使用地图中的值

Go 如何使用地图中的值,go,Go,我想使用我创建的地图中的值与输入所给的天数相乘 我只是不知道如何扫描地图中存储的值 package main import "fmt" func main() { typeCar := map[string]int{ "audi": 50, "volvo": 100, "tesla": 300, } fmt.Print("how many days would you like to rent?: ") v

我想使用我创建的地图中的值与输入所给的天数相乘

我只是不知道如何扫描地图中存储的值

package main

import "fmt"

func main() {

    typeCar := map[string]int{
        "audi":  50,
        "volvo": 100,
        "tesla": 300,
    }

    fmt.Print("how many days would you like to rent?: ")
    var days int
    fmt.Scanf("%d", &days)

    fmt.Println("the price is:", typeCar["audi"]*days, "euro")

    // "typeCar["audi"]" should be the input of the user instead.
}

要在go中迭代地图,请使用range关键字

for key, value := range typeCar {
   //access values here
}

这将为您提供地图中该键的键和值

您可以将用户输入作为字符串获取,并根据映射对其进行测试,以检索关联的值

package main

import "fmt"

func main() {

    typeCar := map[string]int{
        "audi":  50,
        "volvo": 100,
        "tesla": 300,
    }

    fmt.Print("how many days would you like to rent?: ")
    var days int
    fmt.Scanf("%d", &days)

    // "typeCar["audi"]" should be the input of the user instead.
    fmt.Printf("waht type %v ? ", typeCar)
    var userInput string
    fmt.Scan(&userInput)

    tCar, ok := typeCar[userInput]
    if !ok {
        panic("not a valid car")
    }

    fmt.Println("the price is:", tCar*days, "euro")
}
请花点时间熟悉Go的基本工作原理。