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:
C
Output:
C
Output:
func New(text string) errorIt 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)
}
}
Sample ErrorExample 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)
}
}
It Says Error!