README: interface demo
This commit is contained in:
@@ -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:
|
Common Go syntax is already supported. Except for the following, which needs to be improved:
|
||||||
|
|
||||||
* interface (Limited support)
|
|
||||||
* map (Very limited support)
|
* map (Very limited support)
|
||||||
* panic (Limited support)
|
* panic (Limited support)
|
||||||
* recover (Not supported yet)
|
* 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
|
* [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)
|
* [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
|
## Go packages support
|
||||||
|
|||||||
22
_demo/errors/errors.go
Normal file
22
_demo/errors/errors.go
Normal 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())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user