Skip to content

Instantly share code, notes, and snippets.

@xin053
Created July 5, 2019 02:03
Show Gist options
  • Save xin053/e1c79b45fb681e9ae91a6ea349da2ec7 to your computer and use it in GitHub Desktop.
Save xin053/e1c79b45fb681e9ae91a6ea349da2ec7 to your computer and use it in GitHub Desktop.
[go errors]go errors #go #errors

errors.go 源码

package errors

// New returns an error that formats as the given text.
func New(text string) error {
	return &errorString{text}
}

// errorString is a trivial implementation of error.
type errorString struct {
	s string
}

func (e *errorString) Error() string {
	return e.s
}

所以使用 errors.New(string) 便可以创建一个 error, 但是更推荐使用 fmt.Errorf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment