doumu5023 2017-02-08 07:57
浏览 52
已采纳

使用接受io.Writer作为HandleFunc的函数

I would like to make a function that can be a HandleFunc for http but also be called with another writer.

Since http.ResponseWriter implements io.Writer and my function does not need to set HTTP headers, I thought it might be possible:

func doit(w io.Writer, r *http.Request) {
  w.Write([]byte("Hello"))
}

http.HandleFunc("/", doit)

But no:

cannot use doit (type func(io.Writer, *http.Request)) as type func(http.ResponseWriter, *http.Request) in argument to http.HandleFunc

That makes sense, because it would require a type assertion to make an io.Writer compatible with an expected http.ResponseWriter.

Is something like that possible with functions?

  • 写回答

1条回答 默认 最新

  • doulun1939 2017-02-08 08:04
    关注

    Spec: Function types:

    A function type denotes the set of all functions with the same parameter and result types.

    Your doit() function does not qualify to be an http.HandlerFunc because parameter types do not match. The types io.Writer and http.ResponseWriter are 2 completely different types, so the function types taking these types are also different.

    Using an anonymous function value

    However, since the method set of the interface type io.Writer is a subset of the method set of http.ResponseWriter, a value of the latter type can be assigned to the variable of the former type.

    You may wrap it in an anonymous function of type http.HandlerFunc which may simply call doit(), and then you may use it as an http.HandlerFunc:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        doit(w, r)
    })
    

    Anonymous function value with helper function

    If you need this many times, you may create a helper function to produce the http.HandlerFunc function value:

    func wrap(f func(w io.Writer, r *http.Request)) http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
            f(w, r)
        }
    }
    

    And then using it is simply:

    http.HandleFunc("/", wrap(doit))
    

    With custom Handler type

    Another option would be to define your own function type, to which you can attach a simple method to implement http.Handler (namely ServeHTTP()), and that way with a simple type conversion you can register your function as a handler:

    type SimpleHandler func(io.Writer, *http.Request)
    
    func (sh SimpleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        sh(w, r)
    }
    

    Using it:

    http.Handle("/", SimpleHandler(doit))
    

    Note that the expression SimpleHandler(doit) is simply a type conversion, it's not a function call. So there are no new values or anonymous functions created here in the background, this solution is the most efficient.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?