Error handling is a must for any program. The common approach in JavaScript, Python and Java is using try catch statement to handle exceptions, but we don't have exceptions in Go! For example, let's say we are trying to feed Eric Cartman with unhealthy food.
In Python, we would do the following.
deffeed(kid,food):if food.calorie >1000:raiseValueError("this will kill your kid") kid.eat(food)defmain():# Initialize the objects...try:feed(eric, french_fries)exceptValueError:print"please don't do that, he's fat, not big bone"
In Go, we would simply return an error.
funcfeed(k *Kid, f *Food) error {if food.calorie >1000 {return errors.New("this will kill your kid") } k.eat(f)returnnil}funcmain() {// Initialize the objects...if err :=feed(eric, frenchFries); err !=nil { fmt.Println("please don't do that, he's fat, not big bone") log.Error(err) // Log the error }}