package main
import (
"fmt"
"net/http"
"sync"
)
type WaitGroupWrapper struct {
sync.WaitGroup
}
func (w *WaitGroupWrapper) Wrap(cb func()) {
w.Add(1)
go func() {
cb()
w.Done()
}()
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
}
func ListenAndServe() error {
http.HandleFunc("/", HelloHandler)
http.ListenAndServe(":8000", nil)
return nil
}
func main() {
exitCh := make(chan error)
var once sync.Once
exitFunc := func(err error) {
once.Do(func() {
if err != nil {
fmt.Printf("ERR: %s", err)
}
exitCh <- err
})
}
var wg WaitGroupWrapper
wg.Wrap(func() {
exitFunc(ListenAndServe())
})
err := <- exitCh
fmt.Printf("main end err:%v
", err)
}