Command-Line Programs
Validation Errors
A command can validate input and report a message instead of exiting the process.
Validation Errors
validation_errors.go
package main
import "fmt"
func validateCount(count int) string {
if count < 1 {
return "count must be at least 1"
}
if count > 5 {
return "count must be 5 or less"
}
return "ok"
}
func main() {
var count =
message := validateCount(count)
allowed := message == "ok"
fmt.Println("count=", count)
fmt.Println("allowed=", allowed)
fmt.Println("message=", message)
}
package main
import "fmt"
func validateCount(count int) string {
if count < 1 {
return "count must be at least 1"
}
if count > 5 {
return "count must be 5 or less"
}
return "ok"
}
func main() {
var count =
message := validateCount(count)
allowed := message == "ok"
fmt.Println("count=", count)
fmt.Println("allowed=", allowed)
fmt.Println("message=", message)
}
package main
import "fmt"
func validateCount(count int) string {
if count < 1 {
return "count must be at least 1"
}
if count > 5 {
return "count must be 5 or less"
}
return "ok"
}
func main() {
var count =
message := validateCount(count)
allowed := message == "ok"
fmt.Println("count=", count)
fmt.Println("allowed=", allowed)
fmt.Println("message=", message)
}
validation
Validation checks whether a value is acceptable before the program uses it.