douyonglang4845 2016-01-17 01:55
浏览 39

返回void函数并返回另一个void函数的结果?

I'm trying to come up with a very compact way of handling problems that may arise in HTTP requests for a REST API.

There are many conditions that I need to test for, and many potential error responses for the failure of any of these conditions.

I've gotten my handling flow down to something that looks similar to the following:

// Error is actually a method on some struct, so it's only relevant for demonstration purposes.
func Error(w http.ResponseWriter, status int, message string) {
  // Lots of stuff omitted
  w.WriteHeader(status)
  w.WriteJson(r)
}

func HandleSomething(w http.ResponseWriter, r *http.Request) {
  if someCondition != true {
    Error(w, 500, "Some error occurred!")
    return
  }
  if someOtherCondition != true {
    Error(w, 500, "Some other error occurred!")
    return
  }
  if yetAnotherCondition != true {
    Error(w, 500, "Yet another error occurred!")
    return
  }
  if andFinallyOneLastCondition != true {
    Error(w, 500, "One final error occurred!")
    return
  }
  // All good! Send back some real data.
  w.WriteJson(someObject)
}

Since there are often 5-10 conditions that I need to test for, and other errors that can arise during other operations, it would be nice to be able to compact it to the following:

func HandleSomething(w http.ResponseWriter, r *http.Request) {
  if someCondition != true {
    return Error(w, 500, "Some error occurred!")
  }
  if someOtherCondition != true {
    return Error(w, 500, "Some other error occurred!")
  }
  if yetAnotherCondition != true {
    return Error(w, 500, "Yet another error occurred!")
  }
  if andFinallyOneLastCondition != true {
    return Error(w, 500, "One final error occurred!")
  }
  // All good! Send back some real data.
  w.WriteJson(someObject)
}

However, the Go compiler doesn't like this.

It complains both that I'm trying to use the result of Error() as a value, and that I'm trying to return too many arguments. The exact error messages are:

foo.go:41: bar.Response.Error(400, "InvalidRequest", "Error decoding request") used as value
foo.go:41: too many arguments to return

But both Error() and HandleSomething() have the same return signature (e.g. they both return nothing), so shouldn't this work?

It's important that each if statement contains a return, because the function should be exited immediately. If Error() could somehow stop the execution of the calling function, that would also work for me. Sort of like testing.FailNow(), but I believe that relies on Goroutines.

As an aside: I realize that these aren't really "void" functions, but couldn't think of a more appropriate name. Is there a proper name for functions that return nothing in Go?

  • 写回答

2条回答 默认 最新

  • duanlan3598 2016-01-17 02:16
    关注

    C++ allows this. It is very handy for templates.

    But as you see Go does not. I am not sure of the exact steps, but this seems like something you could ask to be added to Go, perhaps it could make it in for 1.7.

    As for a solution right now, perhaps you could return a unused error. Just return it as nil from your Error function.

    评论

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题