UserTypevalidator
type UserType int

const (
    Admin UserType = iota
    Moderator
    ContentCreator
)

func (u UserType) Validate() error {
    switch u {
    case Admin:
        // validate admin
    case Moderator:
        // validate moderator
    case ContentCreator:
        // validate content creator
    default:
        return fmt.Errorf("invalid user type")
    }
    return nil
}

调用VALIDATE将如下所示

func main() {
    a := User{
        Type:         Admin,
        Name:         "admin",
        Password:     "pass",
        LastActivity: time.Time{},
    }

    err := a.Type.Validate()
    if err != nil {
        fmt.Println("invalid user: %w", err)
    }
}