robert-min / project-go

Go 언어 리뷰와 기존에 파이썬으로 진행했던 프로젝트를 Go 언어로 개선한 repo
0 stars 0 forks source link

Error #11

Open robert-min opened 11 months ago

robert-min commented 11 months ago

Error 타입

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")
    }

}