這兩個 Golang 函數(shù)有什么不同,它們彼此相同嗎?func foo1() (ret string) { ret = "hi there" return}func foo2() string { ret := "hi there" return ret}哪個更好?
1 回答

慕沐林林
TA貢獻(xiàn)2016條經(jīng)驗 獲得超9個贊
這兩個函數(shù)是相同的:https ://go.dev/play/p/_6KT5thL2Sj
foo2
使用隱式返回。foo1
使用命名的返回值和naked/bare
返回。有人認(rèn)為這是代碼異味:建議:Go 2: remove bare return
- 1 回答
- 0 關(guān)注
- 83 瀏覽
添加回答
舉報
0/150
提交
取消