Open2

[golang]nilnilはどこまで標準的か

gatapongatapon

https://github.com/Antonboom/nilnil
nilnilREADME.mdでは以下のような記述がある。

return nil, nil is not idiomatic for Go. The developers are used to the fact that if there is no error, then the return value is valid and can be used without additional checks:

参照: https://github.com/Antonboom/nilnil/tree/v0.1.9?tab=readme-ov-file#motivation

DeelL翻訳

return nil, nilはGoのイディオムではない(慣用句的でない)。開発者は、エラーがなければ戻り値は有効であり、追加のチェックなしで使用できるという事実に慣れている

return nil, nilが慣用的でないことは、どこまでgolangコミュニティに受け入れられているのか?

gatapongatapon

結論

標準的ではないし、return nil, nilは標準ライブラリーでも普通に利用されている。
何なら、このnilnilを検出するライブラリーにも return nil, nil の記述がある。
https://github.com/Antonboom/nilnil/blob/master/pkg/analyzer/analyzer.go#L112

golangci-lin に追加された経緯

https://github.com/golangci/golangci-lint/pull/2236

開発のメンバーはこのリンターについて「return nil, nilは標準ライブラリ等で一般的に使われているし、エラーを返したくないシーンが有る」「このlinterには賛同しない」とコメントしている。ただgolangci-lintへの追加については不要な人はdisableにすれば良く、nilnillinterが有益と感じる人もいることを理解しapproveされていた。(そんな感じで追加されるんだ.)