Converting Error to String in Go Lang
In Golang, fmt.Println(err) can print out the error err. But how to convert an error to a string explicitely?
In Go, the error type is an interface type that has an interface
type error interface {
Error() string
}
An error variable represents any value that can describe itself as a string.
Ref: https://golang.org/pkg/builtin/#error
So, for an error err, you can call
err.Error()
to get a string representing err.
For example, the errorString type from errors package of Go lang:
// errorString is a trivial implementation of error.
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
The Go lang’s fmt package formats an error value by calling its Error() string method.
Thank you for sharing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thanks a lot
Thanks a ton!
Nice
Easier than I thought. Thank you!
Today is 03 August 2020 and still your post is saving people like me, Thanks for sharing!!!
May Jesus bless you dude!