Golang:给定其值的常量的名称

rak*_*koo 19 go

你如何得到一个常数的名称给它的价值?

更具体地说(为了获得更易读的理解),我正在使用该crypto/tls软件包.密码套件定义为常量:

const (
    TLS_RSA_WITH_RC4_128_SHA            uint16 = 0x0005
    TLS_RSA_WITH_3DES_EDE_CBC_SHA       uint16 = 0x000a
    TLS_RSA_WITH_AES_128_CBC_SHA        uint16 = 0x002f
    TLS_RSA_WITH_AES_256_CBC_SHA        uint16 = 0x0035
    TLS_ECDHE_RSA_WITH_RC4_128_SHA      uint16 = 0xc011
    TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA  uint16 = 0xc013
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA  uint16 = 0xc014
)
Run Code Online (Sandbox Code Playgroud)

在与服务器成功握手后,我可以通过连接与Ciphersuite达成一致:

c, _ := tls.Dial("tcp", "somedomain.com:443", nil)
// Suppose everything went right

// This is the Ciphersuite for the conn:
cipher := c.ConnectionState().Ciphersuite
Run Code Online (Sandbox Code Playgroud)

cipher这是一个uint16.有没有办法将其显示为字符串,例如,TLS_RSA_WITH_3DES_EDE_CBC_SHA如果这是商定的?

jim*_*imt 22

注意:从Go 1.4开始,String()使用Go的新generate功能并结合stringer命令可以自动生成以下代码.有关详细信息,请参见此处

除了ANisus的回答,您还可以执行以下操作.

package main

import "fmt"
import "crypto/tls"

type Ciphersuite uint16

const (
    TLS_RSA_WITH_RC4_128_SHA            = Ciphersuite(tls.TLS_RSA_WITH_RC4_128_SHA)
    TLS_RSA_WITH_3DES_EDE_CBC_SHA       = Ciphersuite(tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA)
    TLS_RSA_WITH_AES_128_CBC_SHA        = Ciphersuite(tls.TLS_RSA_WITH_AES_128_CBC_SHA)
    TLS_RSA_WITH_AES_256_CBC_SHA        = Ciphersuite(tls.TLS_RSA_WITH_AES_256_CBC_SHA)
    TLS_ECDHE_RSA_WITH_RC4_128_SHA      = Ciphersuite(tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA)
    TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA = Ciphersuite(tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA)
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA  = Ciphersuite(tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA)
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA  = Ciphersuite(tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA)
)

func (cs Ciphersuite) String() string {
    switch cs {
    case TLS_RSA_WITH_RC4_128_SHA:
        return "TLS_RSA_WITH_RC4_128_SHA"
    case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
        return "TLS_RSA_WITH_3DES_EDE_CBC_SHA"
    case TLS_RSA_WITH_AES_128_CBC_SHA:
        return "TLS_RSA_WITH_AES_128_CBC_SHA"
    case TLS_RSA_WITH_AES_256_CBC_SHA:
        return "TLS_RSA_WITH_AES_256_CBC_SHA"
    case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
        return "TLS_ECDHE_RSA_WITH_RC4_128_SHA"
    case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
        return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"
    case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
        return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"
    case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
        return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"
    }
    return "Unknown"
}

func main() {
    cs := TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
    fmt.Printf("0x%04x = %s\n", uint16(cs), cs)

    cs = TLS_RSA_WITH_RC4_128_SHA
    fmt.Printf("0x%04x = %s\n", uint16(cs), cs)

    cs = TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
    fmt.Printf("0x%04x = %s\n", uint16(cs), cs)
}
Run Code Online (Sandbox Code Playgroud)

你可以去游乐场测试它.


ANi*_*sus 17

我担心这是不可能的.
常量在编译时解析,reflect包中没有任何内容允许您检索名称.

我建议创建一个名称为的地图:

var constLookup = map[uint16]string{
    tls.TLS_RSA_WITH_RC4_128_SHA:      `TLS_RSA_WITH_RC4_128_SHA`,
    tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA: `TLS_RSA_WITH_3DES_EDE_CBC_SHA`,
    ...
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ord 8

您可以使用go generate此方法为您生成此内容.

安装stringer使用

go get golang.org/x/tools/cmd/stringer
Run Code Online (Sandbox Code Playgroud)

现在type为你定义一个const.你可以这样做

type TLSType uint16
const (
    TLS_RSA_WITH_RC4_128_SHA            TLSType = 0x0005
    TLS_RSA_WITH_3DES_EDE_CBC_SHA       TLSType = 0x000a
    TLS_RSA_WITH_AES_128_CBC_SHA        TLSType = 0x002f
    TLS_RSA_WITH_AES_256_CBC_SHA        TLSType = 0x0035
    TLS_ECDHE_RSA_WITH_RC4_128_SHA      TLSType = 0xc011
    TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA TLSType = 0xc012
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA  TLSType = 0xc013
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA  TLSType = 0xc014
)
Run Code Online (Sandbox Code Playgroud)

然后将此行添加到您的文件中(间距很重要)

//go:generate stringer -type=TLSType
Run Code Online (Sandbox Code Playgroud)

现在转到程序所在文件夹中的终端并运行

go generate
Run Code Online (Sandbox Code Playgroud)

这将创建一个tlstype_string.go在您的代码目录中调用的文件,该文件是fmt.Stringer为您的代码实现接口的生成代码TLSType.如果你想看到实现但是你不需要打开它.它会起作用.

现在,当您运行go buildgo run *.go在此目录上运行时,源文件将使用生成的代码.

完整的程序看起来像这样

package main

import (
    "fmt"
)

//go:generate stringer -type=TLSType
type TLSType uint16

const (
    TLS_RSA_WITH_RC4_128_SHA            TLSType = 0x0005
    TLS_RSA_WITH_3DES_EDE_CBC_SHA       TLSType = 0x000a
    TLS_RSA_WITH_AES_128_CBC_SHA        TLSType = 0x002f
    TLS_RSA_WITH_AES_256_CBC_SHA        TLSType = 0x0035
    TLS_ECDHE_RSA_WITH_RC4_128_SHA      TLSType = 0xc011
    TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA TLSType = 0xc012
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA  TLSType = 0xc013
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA  TLSType = 0xc014
)

func main() {
    fmt.Println("This const will be printed with its name instead of its number:")
    fmt.Println(TLS_RSA_WITH_RC4_128_SHA)
    fmt.Println()
    fmt.Println("So will this one:")
    fmt.Println(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA)
    fmt.Println()
    fmt.Println("Or maybe we want to give it a value and convert:")
    fmt.Println(TLSType(0xc011))
    fmt.Println()
    fmt.Println("No problem:")
    fmt.Println(TLSType(0xc013))
}
Run Code Online (Sandbox Code Playgroud)

哪个输出

This const will be printed with its name instead of its number:
TLS_RSA_WITH_RC4_128_SHA

So will this one:
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA

Or maybe we want to give it a value and convert:
TLS_ECDHE_RSA_WITH_RC4_128_SHA

No problem:
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
Run Code Online (Sandbox Code Playgroud)

如果添加新const值,只需go generate再次运行即可更新生成的代码.