package calc

import "fmt"

type Number struct {
	Val int
}

const (
	testConst1 = iota
	testConst2
	testConst3
)

// 定义要使用的方法
type funcTest func(n int) *Number

// 方法容器
var testModules = make(map[int]funcTest)

// 初始化 绑定具体的方法
func init() {
	testModules[testConst1] = calc1
	testModules[testConst2] = calc1
	testModules[testConst3] = calc1
}

// 具体实现方法
func calc1(num int) *Number {
	n := &Number{}
	n.Val *= num
	return n
}

// 可以利用容器 依次循环返回值
func Calc() {
	// cost 为容器键 fun 为容器具体内容(这里为函数)
	for cost, fun := range testModules {
		fmt.Println("cost is", cost)
		funRes := fun(cost)
		fmt.Println("funRes is", funRes)
	}
}

  

相关文章:

  • 2021-07-20
  • 2021-08-15
  • 2022-01-02
  • 2021-07-11
  • 2021-08-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
相关资源
相似解决方案