Skip to content

Instantly share code, notes, and snippets.

@gerep
Created December 10, 2014 01:15
Show Gist options
  • Save gerep/9e7639e698d23500c9dc to your computer and use it in GitHub Desktop.
Save gerep/9e7639e698d23500c9dc to your computer and use it in GitHub Desktop.
Go validation
package main
import (
"fmt"
)
type Validation struct {
Error string
OK bool
}
func (v Validation) Message(message string) string {
if v.OK == false {
return message
}
return ""
}
func (v Validation) MinSize(who string, size int) Validation {
if len(who) < size {
return Validation{OK: false}
}
return Validation{OK: true}
}
func (v Validation) MaxSize(who string, size int) Validation {
if len(who) > size {
return Validation{OK: false}
}
return Validation{OK: true}
}
func main() {
v := Validation{}
fmt.Println(v.MinSize("Daniel", 10).Message("Valor é muito pequeno"))
fmt.Println(v.MaxSize("Daniel", 10).Message("Valor é muito grande"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment