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

View File

@@ -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

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())
}