Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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:




// 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!
My Personal Notes arrow_drop_up
Recommended Articles
Page :