2

I have the following code snippent:

type ErrorCode string

const (
    INVALID_REQUEST ErrorCode = "INVALID_REQUEST"
)

type Response struct {
    ErrorCode string `json:"errorCode"`
}

func BuildResponseError(errorCode ErrorCode) string {
    user := &Response{ErrorCode: string(errorCode)}
    response, err := json.Marshal(user)
    if err != nil {
        log.Println(err)
        return `{"errorCode":"bad_request"}`
    }

    return string(response)
}

I can call function BuildResponseError like that:

BuildResponseError("wrong_request")

Is there a way to disable this implicit type conversion? I want to call this function only like this, using a enum value:

BuildResponseError(INVALID_REQUEST)
Max
  • 1,803
  • 3
  • 25
  • 39

1 Answers1

2

You can't disable the possibility to assign a string to a variable of your ErrorCode type since ErrorCode's underlying type is string and according to Go's assignability rules:

A value x is assignable to a variable of type T ("x is assignable to T") if one of the following conditions applies:

...

  • x's type V and T have identical underlying types and at least one of V or T is not a defined type

Source: https://golang.org/ref/spec#Assignability

So that functionality is built-in into the language.

One way to achieve similar functionality (not exactly the same but the closest you can get) would be to define the type like:

type ErrorCode struct {
    Code string
}

And then a variable (you can't define a constant of that type):

var (
    INVALID_REQUEST ErrorCode = ErrorCode{"INVALID_REQUEST"}
)

You should then modify your code, of course, to take the Code from inside that type to build the response:

user := &Response{ErrorCode: errorCode.Code}

That way the function can't be called using a string, only ErrorCode values will be accepted.

Playground: https://play.golang.org/p/qoKrGiJQtxv

eugenioy
  • 11,825
  • 28
  • 35
  • thank you for so good example and explanation. – Max Jun 12 '18 at 21:57
  • Was working on this exact solution, but ended up embedding an anonymous `string` in the struct instead of naming the field, which ends up doing some funky stuff. TIL. – RayfenWindspear Jun 12 '18 at 21:57