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.
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
}
You can do something like this:
var selected *[]int
switch strings.ToLower(set) {
case "a": selected = &a
...
}
*selected = append(*selected, num)