README: interface demo

This commit is contained in:
xushiwei
2024-05-31 08:31:00 +08:00
parent 6fb48023f2
commit 2196f2259f
2 changed files with 23 additions and 1 deletions

22
_demo/errors/errors.go Normal file
View File

@@ -0,0 +1,22 @@
package main
// New returns an error that formats as the given text.
// Each call to New returns a distinct error value even if the text is identical.
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
}
func main() {
err := New("an error")
println(err)
println(err.Error())
}