语法错误函数返回值

huangapple go评论85阅读模式
英文:

syntax error function return value

问题

翻译结果如下:

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)
	}
	
	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

请注意,这只是代码的翻译,不包括错误信息的翻译。

英文:
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

得分: 1

func access_log(r)没有为参数r定义类型。

一旦你定义了类型,编译就应该能够继续进行。

./main.go:83: 返回的参数太多

该函数没有定义返回值,因此出现了错误。

如果你为handlers#LoggingHandler添加返回类型,那就是http.Handler

func access_log(r) http.Handler {
   ...
   // 然后你可以返回:
   return handlers.LoggingHandler(io.Writer(f), r)
}
英文:

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)
}

huangapple
  • 本文由 发表于 2015年4月13日 18:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/29602990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定