// https://play.golang.org/p/LEmzg55qaZ6
package main
import (
"errors"
"fmt"
)
// a form of defer that lets me do something if there is an error and something else if there isnt
var toggleMe = true
func foo() (boo string, err error) {
boo = "boo"
defer func() {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("bar")
}
}()
if toggleMe {
err = errors.New("an error occurred")
}
return
}
func main() {
foo()
fmt.Println("after foo()")
}
java golang oop 2文章片段
// the example is in Java
class Base {
private int i = 0;
void inc1() {
inc2(); // the change
}
void inc2() {
i++;
}
}
class Child extends Base {
@Override
void inc2() {
inc1();
}
}
Child child = new Child();
child.inc2();