errors.New() Function in Golang with Examples
Errors package in Golang is used to implement the functions to manipulate the errors. errors.New()function returns an error that formats like given text. Each call to New returns distinct error value indeed in the event that the content is indistinguishable.
Syntax:
func New(text string) error
It returns an error.
Example 1:
// Golang program to illustrate // the errors.new() function package main import ( "errors" "fmt" ) // Main function func main() { err := errors.New( "Sample Error" ) if err != nil { fmt.Print(err) } } |
Output:
Sample Error
Example 2:
// Golang program to illustrate // the errors.new() function package main import ( "errors" "fmt" ) // Main function func main() { err := errors.New( "It Says Error!" ) if err != nil { fmt.Print(err) } } |
Output:
It Says Error!
Contact Us