doushihu5475 2016-03-30 14:02
浏览 11
已采纳

Golang中的强制返回错误

I want to test how certain code handles errors.

I want a function to return an error.

I have tried typing return 0/0 but then my application won't build

How can I force return an error?

  • 写回答

2条回答 默认 最新

  • duandaotuo5542 2016-03-30 14:06
    关注

    you can return errors like this:

    func ReturnError() (string, error){       
          return "", fmt.Errorf("this is an %s error", "internal server")
          // or
          return "", errors.New("this is an error")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?