[golang]让golang支持泛型编程的一种方案

本博客原创博文版权所有 @[email protected]
仅供交流学习使用用于商业用途请联系原作者 
转载请注明出处:http://blog.sina.com.cn/ally2014

总所周知,golang现在不支持泛型编程,对于习惯了C++泛型编程思想的程序员来说无疑是一个硬伤。
虽然golang interface{}多少可以满足一些泛型需求,但是无论在效率和编程手法上,都无法达到C++template那样的灵活性。出于对编译器效率的考虑,官方暂时还没有golang支持泛型的方案。
根据C++泛型的基本思路,我实现了一个名为gpg[1]的工具,使用工具通过文字替换的方式生成需要的.go代码,解决维护多个相似代码的尴尬,通过配置.gpg(ini)实例化模板文件(.gp),替换相关模板参数
达到维护一份代码(.gp),实现类型无关的编程思想。

如源码中example所示,假如需要实现一个未知类型的全局变量,需要在get和set的时候自动加锁,解决不同goroutine访问全局变量时的冲突。按照现有go规则,使用interface{}每次进行类型断言是一个办法,但是应用起来代码不好看,执行效率也没有原生类型快。
还有一个办法是用到什么类型,就把已经实现的代码拷贝一份改一改类型,这样一旦算法有修改,需要修改所有的实现代码,很不靠谱。
gpg可以解决以上所有问题. gpg的使用方法如下:
gpg tool 使用方法:
gpg-h    显示帮助
gpg       遍历当前目录所有.gpg文件 用同名的.gp文件作为模板生成.go代码文件
gpg 遍历path目录所有.gpg文件 用同名的.gp文件作为模板生成.go代码文件
//////////////////////////////////////////example.gp
//This is an example of using gpg tool forgeneric-programming
//this is an example of using gpg to define an auto-lock globalvalue with generic type
//it will be realized to real go code by gpg tool through the .gpgfile with the same name
package example
import (
 "sync"
)
//auto locked global value
type AutoLockGbl struct {
 val 
 lock sync.RWMutex
}
//new and init a global value
func New(val ) *AutoLockGbl{
 p := &AutoLockGbl{}
 p.val = val
 return p
}
//get value, if modify is disable, lock is unneeded
func (me *AutoLockGbl) Get() (r ) {
 me.lock.RLock()
 defer me.lock.RUnlock()
 r = me.val
 return
}
//set value, if modify is disable, delete this function
func (me *AutoLockGbl) Set(val ) (r ) {
 me.lock.Lock()
 defer me.lock.Unlock()
 r = me.val
 me.val = val
 return
}

//////////////////////////////////////////example.gpg
//分别用int,string,uint64实例化example.gp
;this is exactlly an ini file
;it is used to generate code from .gp file
[int]
TYPE_NAME=Int
VALUE_TYPE=int
LOCK_COMMENT=
 
;禁止修改,不需要锁
[const_str]
TYPE_NAME=ConstStr
VALUE_TYPE=string
LOCK_COMMENT=//
 
;禁止修改,不需要锁
[const_u64]
TYPE_NAME=ConstU64
VALUE_TYPE=uint64
LOCK_COMMENT=//

可以得到形如以下的代码文件
/////////////////////////////////example_gpg_int.go
// This file was auto-generated by [gpg] tool
// Last modify at: 2014-01-21 13:06:46.6378366 +0800 +0800
// !!!!!!!!!NEVER MODIFY IT MANUALLY!!!!!!!!!
//Copyright @Ally 2014. All rights reserved.
//Version:   1.0.0
//Author:    [email protected]
//Blog site: http://blog.sina.com.cn/ally2014
//This is an example of using gpg tool forgeneric-programming
//this is an example of using gpg to define an auto-lock globalvalue with generic type
//it will be realized to real go code by gpg tool through the .gpgfile with the same name
package example
import (
 "sync"
)
//auto locked global value
type AutoLockGblInt struct {
 val  int
 lock sync.RWMutex
}
//new and init a global value
func NewInt(val int) *AutoLockGblInt{
 p := &AutoLockGblInt{}
 p.val = val
 return p
}
//get value, if modify is disable, lock is unneeded
func (me *AutoLockGblInt) Get() (r int) {
 me.lock.RLock()
 defer me.lock.RUnlock()
 r = me.val
 return
}
//set value, if modify is disable, delete this function
func (me *AutoLockGblInt) Set(val int) (r int) {
 me.lock.Lock()
 defer me.lock.Unlock()
 r = me.val
 me.val = val
 return
}
 
/////////////////////////////////example_gpg_const_str.go
// This file was auto-generated by [gpg] tool
// Last modify at: 2014-01-21 13:06:46.6408372 +0800 +0800
// !!!!!!!!!NEVER MODIFY IT MANUALLY!!!!!!!!!
//Copyright @Ally 2014. All rights reserved.
//Version:   1.0.0
//Author:    [email protected]
//Blog site: http://blog.sina.com.cn/ally2014
//This is an example of using gpg tool forgeneric-programming
//this is an example of using gpg to define an auto-lock globalvalue with generic type
//it will be realized to real go code by gpg tool through the .gpgfile with the same name
package example
import (
 //"sync"
)
//auto locked global value
type AutoLockGblConstStr struct {
 val  string
 //lock sync.RWMutex
}
//new and init a global value
func NewConstStr(val string) *AutoLockGblConstStr{
 p := &AutoLockGblConstStr{}
 p.val = val
 return p
}
//get value, if modify is disable, lock is unneeded
func (me *AutoLockGblConstStr) Get() (r string) {
// me.lock.RLock()
// defer me.lock.RUnlock()
 r = me.val
 return
}
//set value, if modify is disable, delete this function
//func (me *AutoLockGblConstStr) Set(val string) (r string) {
// me.lock.Lock()
// defer me.lock.Unlock()
// r = me.val
// me.val = val
// return
//}


这样基本可以满足golang对泛型的需求,而不需要付出额外代价。

本博客原创博文版权所有 @[email protected]
仅供交流学习使用用于商业用途请联系原作者 
转载请注明出处:http://blog.sina.com.cn/ally2014

[1] gpg源码    https://github.com/vipally/gpg

猜你喜欢

转载自blog.csdn.net/vipally/article/details/40952819