Command-Line Programs
Validation Errors
A command can validate input and report a message instead of exiting the process.
validation
Validation checks whether a value is acceptable before the program uses it.
Validation Errors
validation_errors.go
Replay: real traced execution (multi-file project)
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 = 3
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 = 0
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 = 8
message := validateCount(count)
allowed := message == "ok"
fmt.Println("count=", count)
fmt.Println("allowed=", allowed)
fmt.Println("message=", message)
}
count ← 3
15func main() {16 var count→ 3 = 3 //@count=0, 817 message := validateCount(count3)18 allowed := message == "ok"func validateCount(count int) string
5func validateCount(count3 int) string {6 if count < 1 {7 return "count must be at least 1"8 }9 if count > 5 {10 return "count must be 5 or less"11 }12 return "ok"13}message ← "ok", allowed ← true
16 var count = 3 //@count=0, 817 message→ "ok" := validateCount(count3)18 allowed→ true := message"ok" == "ok"1920 fmt.Println("count=", count3)21 fmt.Println("allowed=", allowedtrue)22 fmt.Println("message=", message"ok")23}outputcount= 3 allowed= true message= ok
count ← 0
15func main() {16 var count→ 0 = 017 message := validateCount(count0)18 allowed := message == "ok"func validateCount(count int) string
5func validateCount(count0 int) string {6 if count < 1 {if count < 1
5func validateCount(count int) string {6 if count0 < 1 {7 return "count must be at least 1"8 }message ← "count must be at least 1", allowed ← false
16 var count = 017 message→ "count must be at least 1" := validateCount(count0)18 allowed→ false := message"count must be at least 1" == "ok"1920 fmt.Println("count=", count0)21 fmt.Println("allowed=", allowedfalse)22 fmt.Println("message=", message"count must be at least 1")23}outputcount= 0 allowed= false message= count must be at least 1
count ← 8
15func main() {16 var count→ 8 = 817 message := validateCount(count8)18 allowed := message == "ok"func validateCount(count int) string
5func validateCount(count8 int) string {6 if count < 1 {if count > 5
8}9if count8 > 5 {10 return "count must be 5 or less"11}message ← "count must be 5 or less", allowed ← false
16 var count = 817 message→ "count must be 5 or less" := validateCount(count8)18 allowed→ false := message"count must be 5 or less" == "ok"1920 fmt.Println("count=", count8)21 fmt.Println("allowed=", allowedfalse)22 fmt.Println("message=", message"count must be 5 or less")23}outputcount= 8 allowed= false message= count must be 5 or less