在运行时使用字符串/变量访问常量

我正在研究一个跨平台打字/按键模拟器。在这方面,我正在尝试实现如下功能。


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具有不同的值。对于窗户来说,它是30,对于达尔文来说,它是0x00。


皈依舞
浏览 110回答 1
1回答

HUH函数

您可以使用地图并进行查找 https://blog.golang.org/mapsm = make(map[string]int)m["VK_A"] = 5value := m[constToSelect]地图不是常量
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go