在运行时使用字符串/变量访问常量
Accessing constants using strings / variables during runtime
我正在开发跨平台打字/按键模拟器。在此,我试图实现如下功能。
package main
import "fmt"
import "strings"
const (
VK_A = 5
VK_S = 14
VK_D = 25
)
func main() {
// Suppose, I got user input "a", and based on this,
// i want to print the value of VK_A
var userInput string = "a"
var constToSelect string = "VK_" + strings.ToUpper(userInput)
fmt.Println(constToSelect) // This string is VK_A
// But how can i get 5 which is the value of VK_A
}
我需要这种功能,因为根据平台的不同,VK_A 有不同的值。对于 windows,它是 30,对于 darwin 它是 0x00。
你可以用地图查一下
https://blog.golang.org/maps
m = make(map[string]int)
m["VK_A"] = 5
value := m[constToSelect]
地图不是常量
我正在开发跨平台打字/按键模拟器。在此,我试图实现如下功能。
package main
import "fmt"
import "strings"
const (
VK_A = 5
VK_S = 14
VK_D = 25
)
func main() {
// Suppose, I got user input "a", and based on this,
// i want to print the value of VK_A
var userInput string = "a"
var constToSelect string = "VK_" + strings.ToUpper(userInput)
fmt.Println(constToSelect) // This string is VK_A
// But how can i get 5 which is the value of VK_A
}
我需要这种功能,因为根据平台的不同,VK_A 有不同的值。对于 windows,它是 30,对于 darwin 它是 0x00。
你可以用地图查一下 https://blog.golang.org/maps
m = make(map[string]int)
m["VK_A"] = 5
value := m[constToSelect]
地图不是常量