我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

go中使用多个返回值的惯用错误和值顺序

When creating a func that returns both an error and a result, which order should they be in? I've seen examples of both, and not sure where to look to confirm the idiomatic style.

should it be:

// a
func funcName(n int) (error, int) {}

// or b
func funcName(n int) (int, error) {}

As pointed out by @Gavin above in the comments :

If you look here, you will see By convention, errors are the last return value and have type error, a built-in interface.

As can be seen in the standard lib.

近期文章

  • 无法编译Go文件-“初始化失败…在此块中重新声明了xxxx”
  • 如何在golang自定义结构类型中绑定json字符串化数据? [关闭]
  • gorequest软件包:专门检查超时
  • 没有通道的golang errgroup示例
  • 在另一个文件中进入公共结构
  • 关于Go语言的语法? [重复]
  • 从正则表达式构造字符串
  • Golang变量声明的语法含义[重复]
  • 实例化链码时出错
  • 如何在golang中获取HTTP请求的初始时间戳?
  • 即使文件存在,通过github.com repo进行Golang本地导入也不起作用
  • 读取Json Form数据golang
  • 取消用户特定的goroutines
  • 将字符串转换为字母数字[关闭]
  • 如何对列表的所有元素进行“和”运算符? [关闭]
  • 入门教程:频道和缓冲频道
  • 在go中删除或调整数组大小
  • 附加的第一个参数必须是slice(struct和golang)
  • 转到库以映射json键以清理输出
  • 如何测试以确保函数被调用?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华