How to compare an error message with a string in Golang

Issue

How to compare an error message with a string in golang:

I have a function that returns a struct and an Error, and I must compare the error message with a string, like:

p, err := prod.service.Update(req, int(id))
if err != nil {
    if err == ERROR_UNIQUE_PRODUCT_CODE {
        c.JSON(web.DecodeError(http.StatusConflict, err.Error()))
        return
    } else {
        c.JSON(web.DecodeError(http.StatusNotFound, err.Error()))
        return
    }
}

How can I do it?

Solution

just use

err.Error() == ERROR_UNIQUE_PRODUCT_CODE

Answered By – user19371905

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.