Monday, May 13, 2019

Good Errors Need Your Help

I assume that you have write a program in another language before learning golang. Like c plus plus , Java etc. When a compiler produce exception you can handle it by yourself. In this article i only show you a good errors.

package main
import (
"fmt"
"errors"
)
func main() {
div, err := divide(0,3)
if err != nil{
fmt.Println(div)
}
fmt.Println(div,err)
}
func divide(x , y int )(int, error){
if x == 0 {
return x/y , nil
}
return x/y, errors.New("PASS")
}
If you observe in my above example you see if x equal to zero then it send error while when x not equal to 0 then it send me "Pass" ? errors.New take a string a argument as a parameter and in case any error. This is very productive function when you're working on huge project.
Bad Errors also exit in golang but that will come on right time. Programmer handle bad errors like Java developers handle exceptions.

No comments:

Post a Comment

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Mighty Roman Empire's trail and err

  The Roman Empire's strength, fuel by economical corridor and best men's for the service but Golden days might be over when corrup...

Achieves