Thanks Kevin for your opinion.
Now am assured that variable way works.
I will use that.


On Wed, 6 May, 2020, 11:08 PM 'Kevin Chowski' via golang-nuts, <
golang-nuts@googlegroups.com> wrote:

> I would do (and have done) what you suggested in your last example, but
> just put the codes into a 'var' block as opposed to a const block, e.g.:
>
> var (
>>    E270_01 = ErrorCode{"270.01", "this is error description"}
>
> )
>
>
> I would not personally be concerned about people modifying that public
> value and messing things up because that has never happened within the past
> 5 years of my professional career using Go (on two different teams, working
> with a total of up to 30 other people). At least, as far as I know ;)
>
> If you are concerned about future coders misusing these "effectively
> constant" public values, then you should just use a function to return a
> new copy each time instead of having a global/shared version:
>
> func E270_01() ErrorCode {
>>    return ErrorCode{"270.01", "this is error description"}
>> }
>
>
> In this func example, each time someone needs an ErrorCode to use or
> compare to, they will receive a new copy of ErrorCode, making it impossible
> for misbehaving code to affect other code.
>
> On Monday, May 4, 2020 at 10:30:13 PM UTC-6, Amarjeet Anand 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 golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/f91631d8-0ce9-49de-8fec-ea3383ad8403%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/f91631d8-0ce9-49de-8fec-ea3383ad8403%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAORUUQUo%2BBg2_Jqx_XOTT%3DAeG81Az%3DD9gdDmXrm1qyroa0TTTQ%40mail.gmail.com.

Reply via email to