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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码