diff --git a/README.md b/README.md index 97e95bb9..2c9e95f1 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,6 @@ Here are some examples related to them: Common Go syntax is already supported. Except for the following, which needs to be improved: -* interface (Limited support) * map (Very limited support) * panic (Limited support) * recover (Not supported yet) @@ -183,6 +182,7 @@ Here are some examples related to Go syntax: * [concat](_demo/concat/concat.go): define a variadic function * [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function) +* [errors](_demo/errors/errors.go): demo to implement error interface ## Go packages support diff --git a/_demo/errors/errors.go b/_demo/errors/errors.go new file mode 100644 index 00000000..4e73a70c --- /dev/null +++ b/_demo/errors/errors.go @@ -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()) +}