我是golang的新手。我想得到我的回復作為多個結果。我做了一些方法,但我需要改變那個impartErrl := ph.profileService.ValidateSchema(gojsonschema.NewStringLoader(string(b))) if impartErrl != nil { ctx.JSON(http.StatusBadRequest, impart.ErrorResponse(impartErrl)) return }func (ps *profileService) ValidateSchema(document gojsonschema.JSONLoader) (errors []impart.Error) { result, err := gojsonschema.Validate(ps.schemaValidator, document) if err != nil { ps.SugaredLogger.Error(err.Error()) return impart.ErrorResponse( impart.NewError(impart.ErrBadRequest, "unable to validate schema"), ) } if result.Valid() { return nil } // msg := fmt.Sprintf("%v validations errors.\n", len(result.Errors())) msg := "validations errors" for i, desc := range result.Errors() { msg += fmt.Sprintf("%v: %s\n", i, desc) er := impart.NewError(impart.ErrValidationError, fmt.Sprintf("%s ", desc), impart.ErrorKey(desc.Field())) errors = append(errors, er) } return errors}func NewError(err error, msg string, args ...interface{}) Error { key := GetErrorKey(args...) return impartError{ err: err, msg: msg, key: key, }}func ErrorResponse(err interface{}) []Error { var errorResponse []Error switch err.(type) { case Error: errorResponse = []Error{err.(Error)} case []Error: errorResponse = err.([]Error) default: errorResponse = []Error{ NewError(ErrUnknown, fmt.Sprintf("%v", err)), } } return errorResponse}type Error interface { error HttpStatus() int ToJson() string Err() error Msg() string}我怎樣才能得到這些類型的回應。?因為在ios應用程序中,在解析響應[]時顯示錯誤。所以我需要改變輸出。
1 回答

呼啦一陣風
TA貢獻1802條經驗 獲得超6個贊
函數應返回 a 而不是 。例如:ErrorResponsemap[int]Error[]Error
func ErrorResponse(err interface{}) map[int]Error {
errorResponse := map[int]Error{}
switch e := err.(type) {
case Error:
errorResponse[0] = e
case []Error:
for i, k := range e {
errorResponse[i] = k
}
default:
errorResponse[0] = NewError(ErrUnknown, fmt.Sprintf("%v", err))
}
return errorResponse
}
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消