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