How to return error nil in go

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

How to return error nil in go

问题

我创建了一个返回错误的函数,即使该函数没有错误。我该如何使函数在没有错误时返回nil?

func Serve() error {
   error = nil
   return error
}

类似这样的代码。

英文:

I make a function that return error, even that function not error. How i make return nil if my function not error

func Serve() error {
   error = nil
   return error
}

something like that

答案1

得分: 7

error是一个类型,所以只需返回nil

英文:

error is type, so just

return nil

答案2

得分: 4

func Serve() (err error) {
err = nil
return
}

英文:
func Serve() (err error) {
   err = nil
   return
}

huangapple
  • 本文由 发表于 2016年1月28日 17:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/35056992.html
匿名

发表评论

匿名网友

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

确定