zoukankan      html  css  js  c++  java
  • Golang方法容器

    代码

    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)
    	}
    }
    

      

  • 相关阅读:
    区别Lua模式匹配中 %a+ 与 .-
    将硬件规定的通信协议用Lua实现(涉及到很多Lua通信的数据转换)
    Lua库-string库
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
  • 原文地址:https://www.cnblogs.com/zyfeng/p/15698828.html
Copyright © 2011-2022 走看看