kra*_*ait 5

与C不同,您实际上并不需要函数的"指针",因为在Go中,函数是引用类型,类似于切片,贴图和通道.此外,地址运算符&和产生指向值的指针,但要声明指针类型,请使用*.

您似乎希望您的InitFunc采用单个InitType并且不返回任何值.在这种情况下,您将其声明为:

type InitFunc func(initType)

现在,您的地图初始化可能看起来像:

m := make(map[initType]InitFunc)
package main

import "fmt"

type InitFunc func(initType)
type initType int

const (
    A initType = iota
    B
    C
    D
    MaxInitType
)

func Init1(t initType) {
    fmt.Println("Init1 called with type", t)
}

var initFuncs = map[initType]InitFunc{
    A: Init1,
}

func init() {
    for t := A; t < MaxInitType; t++ {
        f, ok := initFuncs[t]
        if ok {
            f(t)
        } else {
            fmt.Println("No function defined for type", t)
        }
    }
}

func main() {
    fmt.Println("main called")
}

在这里,它循环遍历每个initType,并调用适用的函数(如果已定义).