在Golang中,我们可以为变量名声明一些字符串值吗?

I have slices with some variable names

like

strList := ['abcd', 'efgh', 'ijkl']

and I want to make it to variables names(to make some object iterably) What I curious is that how can I make strings value to variable name. (in code) like strList[0] seems not allowed....

Thanks for your help!

I think you want something like http://play.golang.org/p/M_wHwemWL6 ?

Note that the syntax for a slice literal uses {}'s not []'s.

Since your strings will be read at runtime and your variable names will be checked at compile time, it's probably not possible to actually create a variable with a name based on a string.

However, you can make a map that stores values with string keys. For example, if you wanted to hold integer values inside something you can look up using the values "abcd", "efgh", etc., you would declare:

myMap := map[string]int {
  "abcd": 1,
  "efgh": 2,
  "ijkl": 3,
}

and you could then read those values with e.g. myMap["abcd"] // 1.