4

golang 函数返回值 2 个写法,哪个更好?

 2 years ago
source link: https://www.v2ex.com/t/789079
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Go

golang 函数返回值 2 个写法,哪个更好?

  wangbenjun5 · 1 天前 via Android · 2057 次点击
自己平时写项目,喜欢把返回值 return 回去,比如

所以一般函数都是 function_name()([]struct, error)这种形式。

但是我经常也看到很多人喜欢另一种写法,就是把返回值作为参数传进去,最后只 return error,像这样 function_name(result *[]struct) error

结果都是一样,但是过程不一样,我的那种方式需要在函数里面创建一个 slice,把结果塞进去 return 。但是传参数这种方式,可以直接拿来用,所以理论上第二种方式更高效吗?

大家喜欢哪种写法?

第 1 条附言  ·  1 天前

经过 v 友提醒,我觉得第二种写法主要还是因为很多语言,比如 c,函数是不支持多返回值,估计是习惯,可能以前写 c 写的多

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK