This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
) | |
const ( | |
exitCodeOK int = iota | |
exitCodeFailed | |
) | |
func main() { | |
os.Exit(run(os.Args)) | |
} | |
func run(args []string) int { | |
err := work() | |
if err != nil { | |
log.Println("Failed to execute the work:", err) | |
return exitCodeFailed | |
} | |
return exitCodeOK | |
} | |
func work() error { | |
return nil | |
} |
os.Exit()は、deferを実行しないで処理を終了してしまいます。あっちこっちで使うと思わぬ事故の元。
これをmain関数内だけで使って諸々の処理はその引数であるrun関数内で行うことで、
deferが実行されない事故を防げます。
・・・というのは、書籍「みんなのGo言語」の4.5章の受け売りなんです。
他にも素晴らしい記事がたくさん載ってるので必読!٩( 'ω' )و
0 件のコメント:
コメントを投稿