douxun1407 2015-04-13 10:20
浏览 66
已采纳

语法错误函数返回值

func main(){
    ...

    err := http.ListenAndServe(":9000", access_log(r))
    if err != nil {
        log.Fatal("HTTP server: ", err)
    }
}

func access_log(r http.Handler) {
    f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
    if err != nil {
        log.Panic("Access log: ", err)
    }

    return handlers.LoggingHandler(io.Writer(f), r)
}

error

# command-line-arguments
./main.go:71: access_log(r) used as value
./main.go:83: too many arguments to return
  • 写回答

1条回答 默认 最新

  • dqk42179 2015-04-13 10:25
    关注

    func access_log(r) does not define the type for the parameter r.

    Once you define it, the compilation should be able to proceed.

    ./main.go:83: too many arguments to return
    

    The function as define as no return value, hence the error.

    If you add the return type of handlers#LoggingHandler, that would be http.Handler.

    func access_log(r) http.Handler {
       ...
       // Then you can return:
       return handlers.LoggingHandler(io.Writer(f), r)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?