mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 17:07:18 +08:00
98 lines
1.4 KiB
Go
98 lines
1.4 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
const (
|
|
StatusSuccess = "success"
|
|
StatusErrorFoo = "error_foo"
|
|
StatusErrorBar = "error_bar"
|
|
)
|
|
|
|
func main() {
|
|
_ = f1()
|
|
_ = f2()
|
|
_ = f3()
|
|
}
|
|
|
|
func f1() error {
|
|
var status string
|
|
defer notify(status)
|
|
defer incrementCounter(status)
|
|
|
|
if err := foo(); err != nil {
|
|
status = StatusErrorFoo
|
|
return err
|
|
}
|
|
|
|
if err := bar(); err != nil {
|
|
status = StatusErrorBar
|
|
return err
|
|
}
|
|
|
|
status = StatusSuccess
|
|
return nil
|
|
}
|
|
|
|
func f2() error {
|
|
var status string
|
|
defer notifyPtr(&status)
|
|
defer incrementCounterPtr(&status)
|
|
|
|
if err := foo(); err != nil {
|
|
status = StatusErrorFoo
|
|
return err
|
|
}
|
|
|
|
if err := bar(); err != nil {
|
|
status = StatusErrorBar
|
|
return err
|
|
}
|
|
|
|
status = StatusSuccess
|
|
return nil
|
|
}
|
|
|
|
func f3() error {
|
|
var status string
|
|
defer func() {
|
|
notify(status)
|
|
incrementCounter(status)
|
|
}()
|
|
|
|
if err := foo(); err != nil {
|
|
status = StatusErrorFoo
|
|
return err
|
|
}
|
|
|
|
if err := bar(); err != nil {
|
|
status = StatusErrorBar
|
|
return err
|
|
}
|
|
|
|
status = StatusSuccess
|
|
return nil
|
|
}
|
|
|
|
func notify(status string) {
|
|
fmt.Println("notify:", status)
|
|
}
|
|
|
|
func incrementCounter(status string) {
|
|
fmt.Println("increment:", status)
|
|
}
|
|
|
|
func notifyPtr(status *string) {
|
|
fmt.Println("notify:", *status)
|
|
}
|
|
|
|
func incrementCounterPtr(status *string) {
|
|
fmt.Println("increment:", *status)
|
|
}
|
|
|
|
func foo() error {
|
|
return nil
|
|
}
|
|
|
|
func bar() error {
|
|
return nil
|
|
}
|