Created
February 23, 2026 17:45
-
-
Save ricardocuellar/a15a2e873bb34088ea022c1c1fac4267 to your computer and use it in GitHub Desktop.
This is the types.go of section 09 - Curso Golang
This file contains hidden or 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" | |
| "regexp" | |
| "strings" | |
| ) | |
| type Email string | |
| // Money en centavos | |
| type Money int64 | |
| var emailRe = regexp.MustCompile(`^[^\s@]+@[^\s@]+\.[^\s@]+$`) | |
| func NewEmail(v string) (Email, error) { | |
| v = strings.TrimSpace(strings.ToLower(v)) | |
| if !emailRe.MatchString(v) { | |
| return "", fmt.Errorf("email inválido: %q", v) | |
| } | |
| return Email(v), nil | |
| } | |
| func NewMoneyFromCents(cents int64) (Money, error) { | |
| if cents < 0 { | |
| return 0, fmt.Errorf("money no puede ser negativo") | |
| } | |
| return Money(cents), nil | |
| } | |
| func (m Money) String() string { | |
| d := int64(m) / 100 | |
| c := int64(m) % 100 | |
| return fmt.Sprintf("$%d.%02d", d, c) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment