Created
December 10, 2014 01:15
-
-
Save gerep/9e7639e698d23500c9dc to your computer and use it in GitHub Desktop.
Go validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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