golang中map的用法存储函数

栏目: Go · 发布时间: 6年前

内容简介:执行结果
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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Weaving the Web

Weaving the Web

Tim Berners-Lee / Harper Paperbacks / 2000-11-01 / USD 15.00

Named one of the greatest minds of the 20th century by Time , Tim Berners-Lee is responsible for one of that century's most important advancements: the world wide web. Now, this low-profile genius-wh......一起来看看 《Weaving the Web》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具