douhan5853 2019-08-26 22:24
浏览 111
已采纳

使用其他变量中的数据引用go中的变量?

I am relatively new to go/ coding.I want to be able to reference a variable by using a variable.

var a = make([]int,0)
var b = make([]int,0)
var c = make([]int,0)

I have some slices defined

set := input.Ask("Which set would you like to input to (a, b, c):")
for true {
  num := input.Ask("Number:")
  strings.toLower(set) = append(strings.toLower(set), num)
}

I want the "strings.tolower" part output a string, which it does, that will allow me to select one of the variables I have defined.

  • 写回答

2条回答 默认 最新

  • dtgu21994537 2019-08-26 23:37
    关注

    This sounds like the general description of a string-keyed map: you have three values, and you want to choose which one to use based on some string value.

    maps := map[string][]int{}
    
    set := input.Ask("Which set would you like to input to:")
    set = strings.toLower(set)
    for true {
        num := input.Ask("Number:")
        maps[set] = append(maps[set], num)
    }
    

    If it's important to you to only be able to use one of these three values, you can prepopulate the map

    maps := map[string][]int{
        "a": nil,
        "b": nil,
        "c": nil,
    }
    set := input.Ask("Which set would you like to input to (a, b, or c):")
    set = strings.toLower(set)
    if _, ok := maps[set]; !ok {
        fmt.Println("I don't know about that set")
        return
    }
    

    There's more information on maps in the language spec.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题