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.
Bad Errors also exit in golang but that will come on right time. Programmer handle bad errors like Java developers handle exceptions.
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.package mainimport ("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")}
Bad Errors also exit in golang but that will come on right time. Programmer handle bad errors like Java developers handle exceptions.
Comments
Post a Comment