errors.New() Function in Golang with Examples
Last Updated :
03 May, 2020
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:
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New( "Sample Error" )
if err != nil {
fmt.Print(err)
}
}
|
Output:
Sample Error
Example 2:
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New( "It Says Error!" )
if err != nil {
fmt.Print(err)
}
}
|
Output:
It Says Error!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...