Declaring map with function pointer value in goLang

huangapple go评论87阅读模式
英文:

Declaring map with function pointer value in goLang

问题

我想声明一个map,它看起来像这样,这样我就可以将各种init函数映射到initType

func makeMap(){
    m := make(map[initType]InitFunc)
    // 这个map的值声明应该如何设置?
}

type initType int 

const(
    A initType = iota
    B
    C
    D
)

type InitFunc func(initType)

func initA(aInitType initType){
    doStuff(aInitType)
}

func initB(aInitType initType){
    doOtherStuff(aInitType)
}

func initC(aInitType initType){
    doMoreStuff(aInitType)
}

我将函数指针类型声明为InitFunc(在示例中我称之为&InitFunc,因为我不知道如何声明),这样我就可以将其作为Map的值使用。

英文:

I'd like to declare a map that would that would look like this, so I could map various init functions to initType:

func makeMap(){

	m := make(map[initType]&InitFunc)
    //How should the value declaration be set up for this map?

}


type initType int 

const(
	A initType = iota
	B
	C
	D
)


func init(aInitType initType){
	doStuff(aInitType)

}


func init(aInitType initType){
	doOtherStuff(aInitType)

}


func init(aInitType initType){
	doMoreStuff(aInitType)

}

How do I declare the function pointer type (which I called &InitFunc in the example because I don't know how to do it) so I can use it as the value in a Map?

答案1

得分: 5

与C语言不同,Go语言中实际上不需要一个“指针”来指向函数,因为在Go语言中,函数是引用类型,类似于切片、映射和通道。此外,地址运算符&会产生一个指向值的指针,但要声明指针类型,请使用*。

你似乎希望你的InitFunc接受一个InitType参数并且不返回任何值。在这种情况下,你可以这样声明它:

type InitFunc func(initType)

现在,你的映射初始化可以简单地如下所示:

m := make(map[initType]InitFunc)

一个完整的示例可以是(http://play.golang.org/p/tbOHM3GKeC):

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,并调用适用的函数(如果已定义)。

英文:

Unlike C, you don't actually need a "pointer" to the function, since in Go, functions are reference types, similar to slices, maps, and channels. Further, the address operator, &, produces a pointer to a value, but to declare a pointer type, use *.

You seem to be wanting your InitFunc to take a single InitType and return no values. In that case, you would declare it as:

type InitFunc func(initType)

Now, your map initialization can simply look like:

m := make(map[initType]InitFunc)

A complete example would be (http://play.golang.org/p/tbOHM3GKeC):

package main

import &quot;fmt&quot;

type InitFunc func(initType)
type initType int

const (
	A initType = iota
	B
	C
	D
	MaxInitType
)

func Init1(t initType) {
	fmt.Println(&quot;Init1 called with type&quot;, t)
}

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

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

func main() {
	fmt.Println(&quot;main called&quot;)
}

Here, it's looping through each initType, and calling the applicable function, if it is defined.

huangapple
  • 本文由 发表于 2014年1月29日 05:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/21417175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定