代码实例:
package main
import (
"runtime"
"sync"
"os"
"os/signal"
"syscall"
"fmt"
)
//定义函数类型
type Msg func(name string) string
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
wg := &sync.WaitGroup{}
c := make(chan os.Signal, 1)
handleMap := make(map[int]Msg)
handleMap[1] = handle1
handleMap[2] = handle2
handleMap[3] = handle3
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
sig := <-c
_ = sig
s := handleMap[3]
s("测试")
wg.Done()
}()
wg.Add(1)
fmt.Println("执行任务~~~")
wg.Wait()
fmt.Printf("结束")
}
func handle1(name string) string {
fmt.Println(name)
return "handle1"
}
func handle2(name string) string {
fmt.Println("handle2")
return "handle2"
}
func handle3(tt string) string {
fmt.Println(tt)
return "handle3"
}
执行结果:
执行任务~~~
测试
结束
Process finished with exit code 0