Error handling intro

nil is a predeclared identifier like true, false, len(), int32, float64 etc. Since it is a predeclared identifier, it can be used anywhere without importing any package. nil value means that the value is not initialized yet. It is similar to following identifiers in other languages: null // JavaScript None // Python null // Java nil // Ruby The zero value of all pointer-based types in Go is nil. Following are the pointer-based types in Go: [Read More]

Error handling intro

Error is by far the most used interface in Go. lets have a look at it: type error interface { Error() string } //The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. So any type that implements the Error method is by default an error type. Some Examples: package main import "fmt" type MyError string func (me MyError) Error() string{ return string(me) //cast it back to string } type MyOtherError struct{ Code int Message string } func (me MyOtherError) Error() string{ return fmt. [Read More]

error vs errors

Handling errors is a crucial part of writing robust programs. When scanning the Go packages, it is not rare to see APIs which have multiple return values with an error among them. For example: func Open(name string) (*File, error) Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError. [Read More]