登录
首页 >  Golang >  Go问答

利用地图数据进行分析

来源:stackoverflow

时间:2024-03-11 08:45:17 372浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《利用地图数据进行分析》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我想使用我创建的地图中的值来乘以输入给出的天数。

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

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.
}

解决方案


您可以获取字符串形式的用户输入,并根据地图对其进行测试以检索关联的值。

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")
}

以上就是《利用地图数据进行分析》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>