go-errgenæ¸ãã
goã®structã«ã¨ã©ã¼å®ç¾©ãæ¸ãã¦ããã°è¯ãæãã§ãã¨ã©ã¼ãè¿å´ããé¢æ°ããã³ã¼ãçæãããã¼ã«ã§ããgo-errgenãæ¸ãã¾ããï¼
Synopsisã«æ¸ãã¦ããã¨ããï¼
package mypkg //go:generate errgen -type=myErrors type myErrors struct { FooErr error `errmsg:"this is FOO error"` BarErr error `errmsg:"this is BAR error [%d, %s]" vars:"hoge int, fuga string"` }
ã¿ãããªæãã§structã«ã¨ã©ã¼å®ç¾©ãæ¸ãã¦ï¼go:generate
ãè¨å®ãã¦ãã go generate
ãå®è¡ããã¨
package mypkg import "errors" import "fmt" func FooErr() error { return errors.New("[ERR-1] this is FOO error") } func BarErr(hoge int, fuga string) error { return fmt.Errorf("[ERR-2] this is BAR error [%d, %s]", hoge, fuga) } func MyErrorsList() []string { return []string{ `[ERR-1] this is FOO error`, `[ERR-2] this is BAR error [%d, %s]`, } }
ã¨ããæãã®ã³ã¼ãã my_errors_errmsg_gen.go
ã¨ãã¦çæãããã¨ãããã¼ã«ã§ãï¼
ããã¯ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãéãçªå·ãä»ããprefixããä»ä¸ããerrorãè¿å´ããé¢æ°ãã³ã¼ãçæãã¾ãï¼ãã errmsg
ã«å ã㦠vars
ãã©ã¡ã¼ã¿ãå®ç¾©ããã¦ããå ´åã¯ãã®å¤ãé¢æ°ã®å¼æ°ãã©ã¡ã¼ã¿ã¨ãã¦å©ç¨ããï¼ã㤠fmt.Errorf()
ã«ãã£ã¦sprintfäºæã®ãã¬ã¼ã¹ãã«ãã«bindããã¾ãï¼
主ãªã¢ããã¼ã·ã§ã³ã¨ãã¦ã¯ï¼
- é権çã«ã¨ã©ã¼ãå®ç¾©ãã¦ç®¡çããã (ã¨ã©ã¼å®ç¾©ãæ£å¨ããã¨ã¤ãã)
- ã¨ã©ã¼ã®ç¹å®ãè¡ãéã«ã¨ã©ã¼ã³ã¼ããå©ç¨ããã
ã¨ããã®ãããï¼ç¹ã«å¾è ã¯ãããããªãã¼ã ããå¤ãå©ç¨ãããã³ã³ãã¼ãã³ãã§ããã°å¿ é ã«è¿ãæ©è½ã§ãããï¼ã§ï¼é権çã«ã¨ã©ã¼ã管çããã«ãã¦ãï¼æã§ã¨ã©ã¼ã³ã¼ããè¨è¿°ããããã«ããã¨ããã§ãã£ãããã¹ã£ã¦ãã¾ãå¯è½æ§ãããã¾ãã (ä¾ãã°ã¨ã©ã¼ã³ã¼ããéè¤ããã¦ãã¾ãã¨ã)ï¼ãããã¨ããã¯æ©æ¢°çã«ãããããªãã¨ããæ°æã¡ããerrgenãä½ã£ãã¨ããæãã§ãï¼
errgen -type=myErrors -prefix=My-Prefix
ã®ããã« -prefix
ãä»ä¸ãã㨠[My-Prefix-1]
ã®ããã«prefixãèªç±ã«è¨å®ãããã¨ãã§ãã¾ãï¼
ãã®æã®ä»çµã¿ã¯ããã¸ã§ã¯ããã¨ã«èªä½ããã¡ã ã£ããã§ããï¼æ¯åº¦æ¯åº¦æ¸ãã¨ããã®ãé¢åã ã£ãã®ã§ãã®åº¦æ±ç¨çã«ä½¿ãããããã¼ã«åããã¨ãã次第ã§ãï¼
ãå©ç¨ãã ããã¾ãï¼