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

图片转代码服务由CSDN问答提供 功能建议

  func main(){
 ... 
 
 err:= http.ListenAndServe(  “:9000”,access_log(r))
,如果错误!= 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)
如果err!= nil {
 log.Panic(“访问日志:  “,err)
} 
 
返回处理程序。LoggingHandler(io.Writer(f),r)
} 
   
 
 

错误
 #命令行参数
./main.go:71:access_log(r)用作值
./main.go:83:参数太多,无法返回
    
 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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)
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题