Or I will have to go like this?
type ErrCode struct {
Code string
Desc string
}
var (
E100_01 = ErrCode{"E100_01", "this is description"}
E100_02 = ErrCode{"E100_02", "this is description"}
)
On Tue, May 5, 2020 at 5:51 PM Amarjeet Anand <[email protected]> wrote:
> Hi Kurtis
> Thanks for the response.
>
> As the doc says, *stringer* can be used to generate only *int* constants.
> String constants are not supported.
> In my case, the constant will be like -
>
> type ErrCode string
> const (
> E201_01 ErrCode = "description text"
> )
>
>
>
> Actually I need both the strings(*E201_01* and "*description text"*)
> accessible in my code.
> Is this also achievable using generate/stringer?
>
>
>
>
> On Tue, May 5, 2020 at 10:14 AM Kurtis Rader <[email protected]> wrote:
>
>> See https://blog.golang.org/generate and
>> https://godoc.org/golang.org/x/tools/cmd/stringer for an example of a
>> now standard Go tool that seems to do what you want.
>>
>> On Mon, May 4, 2020 at 9:30 PM Amarjeet Anand <[email protected]>
>> wrote:
>>
>>> Hi
>>>
>>> I want to declare a constant that maps an *ErrorCode*(string) like
>>> "100.01" to its *ErrorDescription*(string) like "Error description of
>>> 100.01".
>>> Declaring Error as *code* and *description* is helpful to monitor logs
>>> based based on *ErrorCode* and show the *ErrorDescription* to the
>>> client.
>>>
>>> Although go cannot create constant of type map, but it can be achieved
>>> in multiple ways.
>>>
>>> --------------------------------------------------------------------
>>> --------------------------------------------------------------------
>>> --------------------------------------------------------------------
>>> -------------
>>> One possible way can be :-
>>>
>>>
>>> type ErrorCode string
>>>
>>> const (
>>> E270_01 ErrorCode = "270.01"
>>> E270_02 = "270.02"
>>> )
>>>
>>> var ErrDescription = map[ErrorCode]string{
>>> E270_01: "this is error description",
>>> E270_02: "this is error description",
>>> }
>>>
>>> type LogErr struct {
>>> Code ErrorCode
>>> Description string
>>> }
>>>
>>> func getLogErr(e ErrorCode) LogErr {
>>> return LogErr{
>>> Code: e,
>>> Description: ErrDescription[e],
>>> }
>>> }
>>>
>>> func TestErrorConstant(t *testing.T) {
>>> fmt.Println(getLogErr(E270_01))
>>> }
>>>
>>>
>>>
>>> This solves our purpose. But the problem is for every new error, we need to
>>> change things at two places, (1) Declare const like E270_02 (2) Add an
>>> entry in the *ErrDescription* map
>>>
>>>
>>>
>>> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>> Another possible way looks like :-
>>>
>>>
>>> type ErrorCode string
>>>
>>> const (
>>> E270_01 ErrorCode = "270.01:this is error description"
>>> E270_02 = "270.02:this is error description"
>>> )
>>>
>>> type LogErr struct {
>>> Code string
>>> Description string
>>> }
>>>
>>> func getLogErr(e ErrorCode) LogErr {
>>> * token := strings.Split(string(e), ":")*
>>> return LogErr{
>>> Code: token[0],
>>> Description: token[1],
>>> }
>>> }
>>>
>>> func TestErrorConstant(t *testing.T) {
>>> fmt.Println(getLogErr(E270_01))
>>> }
>>>
>>>
>>>
>>> This way looks promising, but don't really like the way of splitting string
>>> using ":"
>>>
>>>
>>>
>>>
>>> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>> I think best way could have been something like ---
>>>
>>> const (
>>> E270_01 ErrorCode = {"270.01", "this is error description"}
>>> )
>>>
>>>
>>>
>>> Since Golang doesn't support the Constant of struct, what could be your
>>> approach?
>>>
>>> Any suggestion is really appreciated.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/198455c0-4f81-4e2f-a166-72796a15d498%40googlegroups.com
>>> <https://groups.google.com/d/msgid/golang-nuts/198455c0-4f81-4e2f-a166-72796a15d498%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/CAORUUQW8Jh2-YvSDapTzWSrjzxWxqjLmYA5t%2BHFjZv_ZtfSgwg%40mail.gmail.com.