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

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

查看所有标签

猜你喜欢:

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

Algorithms on Strings, Trees and Sequences

Algorithms on Strings, Trees and Sequences

Dan Gusfield / Cambridge University Press / 1997-5-28 / USD 99.99

String algorithms are a traditional area of study in computer science. In recent years their importance has grown dramatically with the huge increase of electronically stored text and of molecular seq......一起来看看 《Algorithms on Strings, Trees and Sequences》 这本书的介绍吧!

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

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具