Go è¨èªã«ã¯ä¾å¤æ©æ§ãåãã£ã¦ããããé¢æ°ãæç¶ãã®ã¨ã©ã¼çãªç¶æ³ã表ãã«ã¯ãè¿ãå¤ãå¤å¤ã«ãã¦æ¬æ¥èå³ããçµæã¨ã¨ãã« error ã¤ã³ã¿ã¼ãã§ã¤ã¹ãè¿ããã¨ããã®ãä¸è¬çã§ããä¾ããããã¾ã§ããªãã§ããããã¡ã¤ã«ãéãã¨ããï¼å¤±æããå¯è½æ§ã®ããï¼å¦çãè¡ããªãããããæã:
f, err := os.Open(filename) if err != nil { // handle err }
ä½ãã¨ãæã«æ¸ãä¸ããã¨ãæ±ãã Go ãããä»æ§ã§ãããæå¥ãã人ãå¤ããã§ãããå¼ã³åºãå´ãç°å¸¸ç³»ãæèããããå¾ãªããªãã®ã§ãããå¹æã大ããã¨æãã¾ãã
å ã®ä¾ã®ããã«ãã¨ã©ã¼ãçºçããããããªå¦çãå¼ã³åºãããã¨ã¯ err ããã§ãã¯ãããã¨ããã®ã¯æåã«å¦ã¶ã¤ãã£ãªã ã¨è¨ã£ã¦ããããããããæ¸ããã¨ã«ãªãã³ã¼ãã§ãããã»ãã¨ãã«ä½åº¦ãæ¸ããã¨ã«ãªãã®ã§ããã¯é¢åã
go-iferr
ããã§ãã¡ãããããã°ã©ã ã«ã³ã¼ããæ¸ããã試ã¿ã
https://github.com/motemen/go-iferr
goiferr
ã¯ä½ããã®ã¨ã©ã¼ãåãåãå¦çã®ç´å¾ã«ããã®ã¨ã©ã¼ã®å¦çãè¡ãããã¤ãã®ãã³ã¼ãããããªã«æ¿å
¥ãã¦ããããã¼ã«ã§ããgo get
ã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
go get -u github.com/motemen/go-iferr/cmd/goiferr
ä¾ãè¦ãã®ãæã£åãæ©ãã§ããã:
package eg import "os/exec" func GoVersion() (string, error) { path, err := exec.LookPath("go") cmd := exec.Command(path, "version") b, err := cmd.Output() return string(b), nil }
ä¾ãã°ãã®ãã㪠"go version" ã³ãã³ãã®åºåãæååã¨ãã¦è¿ãã³ã¼ããæ¸ããã¨ãã¾ããããã¨ã©ã¼ãã§ãã¯ããµãã£ã¦ãã¦ä¸éå端ã§ãããã¨ããããã®ã¾ã¾ã§ã¯ã³ã³ãã¤ã«ãéããªãã®ã§ã¯ã
ãã®ã½ã¼ã¹ã³ã¼ãã a.go
ã¨ããååã§ä¿åããgoiferr
ã«å¦çããã¾ãã
% goiferr a.go > b.go main.go:31: error (ignored): a.go:6:8: err declared but not used === a.go % diff -u a.go b.go
--- a.go 2015-12-18 13:06:27.000000000 +0900 +++ b.go 2015-12-18 13:06:43.000000000 +0900 @@ -4,9 +4,15 @@ func GoVersion() (string, error) { path, err := exec.LookPath("go") + if err != nil { + return "", err + } cmd := exec.Command(path, "version") b, err := cmd.Output() + if err != nil { + return "", err + } return string(b), nil }
ãã便å©ãerr
ã®ãã§ãã¯ããããªã«ãããªã£ã¦ããã¾ããï¼ ããè¦ãã¨ã©ã¼å¦çã追å ããã¦ãå®å¿æãããã¾ããã
å¼æ°ã«ãã£ã¬ã¯ããªãããã±ã¼ã¸åãä¸ããã¨è¤æ°ã®ãã¡ã¤ã«ã対象ã«åãå¦çãè¡ãªãã¾ãã-w
ãªãã·ã§ã³ãä¸ããã¨ãã¡ã¤ã«ãç´æ¥ä¸æ¸ããã¾ãã®ã§ãæ®éã¯ãããå©ç¨ããã®ã便å©ã§ãã
ç´°ããæå
ã¨ã©ã¼å¦çãæ¿å ¥ãããç®æ
goiferr
ã¯ãerror
åããã¤å¤æ°ã左辺ã«ç»å ´ãã代å
¥æããããã«å¯¾ãã¦ã代å
¥æã¨ãã®æ¬¡ã®æã¨ã®éã1è¡ä»¥ä¸ç©ºãã¦ããå ´åãã¨ã©ã¼å¦çãæ¿å
¥ãã¾ããã¤ã¾ã
x, err := getX() x.Foo()
ã®ãããªã½ã¼ã¹ã«ã¯ä½ãå¤æ´ãå ãã¾ãããã
x, err := getX() x.Foo()
ã¨ãªã£ã¦ããã¨ã¨ã©ã¼å¦çãæ¿å ¥ãã¾ãã
ã¨ã©ã¼å¦çã®å 容
ã¨ã©ã¼å¦çã¯
if err != nil { ... }
ã¨ããå½¢ã§ãã³ã³ããã¹ãã«ãã£ã¦ if æã®å 容ãå¤ããã¾ãã
ã¾ãå½è©²ã®æã error
ãè¿ãå¤ã«é¢æ°ã®ä¸ã«ããå ´åãerr
ãè¿ããã㪠return æã«ãªãã¾ããããã¯å
ã»ã©ã® GoVersion
é¢æ°ã®ä¾ã«ããã¨ããã
ã¾ããã¹ã³ã¼ããã log
ããã±ã¼ã¸ãåç
§ã§ããå ´åã«ã¯ log.Fatalf(err)
ãã¾ãã
import "log" func main() { _, err := GoVersion() + if err != nil { + log.Fatal(err) + } + }
t
ã¨ãã *testing.T
åããã¤å¤æ°ãåç
§ã§ããå ´åã«ã¯ãã¹ãé¢æ°ã®ä¸ã ã¨ã¿ãªãã¦ãt.Fatalf(err)
ãã¾ãã
import "testing" func TestFoo(t *testing.T) { _, err := GoVersion() + if err != nil { + t.Fatal(err) + } + }
以ä¸ã®ã©ã®æ¡ä»¶ã«ãåè´ããªããã°ãpanic(err.Error())
ãã¾ãã
func main() { _, err := GoVersion() + if err != nil { + panic(err.Error()) + } + }
ããã§ãã¦ã¾ããã
https://github.com/motemen/go-iferr
以ä¸ãGo ãæ¸ãä¸ã§é¢åãªã¨ãããèªååãã goiferr ã®ç´¹ä»ã§ããï¼ ä»ã®ã¨ãã便å©ã§ããããã¨ããããä½ã£ã¦ã¿ããã¬ãã«ãªã®ã§ä»¥ä¸ã®ããã«ã¾ã ã¾ã ããã¹ããã¨ã¯æ®ã£ã¦ãã¾ããã³ã³ãã¤ã«ã®éããªãã³ã¼ããçæãããã¨ããããã¨æãã¾ãããããããç´ãã¦ããã¤ããã§ãã
TODO
- é¢æ°ã®è¿ãå¤ãååä»ãã®å ´åã®å¯¾å¦
err
以å¤ã®å¤æ°åã許å¯ãã- ã¨ã©ã¼å¦çã®ã³ã¼ããã«ã¹ã¿ãã¤ãºå¯è½ã«ãã