Name of a constant given its value

前端 未结 3 847
名媛妹妹
名媛妹妹 2021-02-01 04:10

How do you get the name of a constant given its value ?

More specifically (and to get a more readable understanding), I\'m working with the crypto/tls packa

3条回答
  •  误落风尘
    2021-02-01 04:42

    I am afraid that is not possible.
    The constants are resolved at compile time and there is nothing in the reflect package that allows you to retrieve the name.

    What I suggest is creating a map with the names:

    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`,
        ...
    }
    

提交回复
热议问题