Open robert-min opened 11 months ago
Error
type error interface { Error() string }
package main import "fmt" type PasswordError struct { Len int RequireLen int } // Error 매서드 정의 func (err PasswordError) Error() string { return "the password len very short." } func RegisterAccount(name, password string) error { if len(password) < 8 { return PasswordError{len(password), 8} } return nil } func main() { err := RegisterAccount("myId", "mypw") if err != nil { if errInfo, ok := err.(PasswordError); ok { fmt.Printf("%v Len: %d RequiredLen:%d\n", errInfo, errInfo.Len, errInfo.RequireLen) } } else { fmt.Println("Success sign") } }
Error 타입
Error
로 구성