[Golang] allows Golang to support a scenario implementation of generic programming

Source: Internet
Author: User
Tags gpg
This is a creation in Article, where the information may have evolved or changed. the original blog post copyright @Allyvipally @gmail.com please contact the original author for commercial use only for Exchange learning . Reprint Please specify Source: http://blog.sina.com.cn/ally2014
It is generally known that Golang now does not support generic programming, which is undoubtedly a mishap for programmers accustomed to C + + generic programming ideas. Although Golang interface{} can meet a number of generic requirements, the flexibility of c++template is not achieved in both efficiency and programming. For the sake of compiler efficiency, there is no official Golang support for generics at the moment. Based on the basic idea of C + + generics, I implemented a tool called gpg[1] that uses tools to generate the required. Go code by using a text substitution to resolve the embarrassment of maintaining multiple similar codes by configuring the. GPG (INI) instantiation template file (. gp), Replace the relevant template parameters to maintain a copy of the code (. gp), implementing type-independent programming ideas.
As example in the source code shows, if you need to implement an unknown type of global variables, you need to automatically lock in get and set, to resolve the different goroutine access to global variables when the conflict. According to the existing GO rules, using interface{} to make a type assertion is a method, but the code is not good to use, and the efficiency of execution is not as fast as the native type. There is also a way to use what type, the code has been implemented to copy a modified type, so that once the algorithm has been modified, need to modify all the implementation code, very unreliable. GPG solves all of these problems. The use of GPG is as follows: GPG tool How to use: gpg-h Display Help GPG Traverse the current directory all. gpg files are generated as templates with the. gp file of the same name. Go code file GPG traverse the path directory all. gpg files are generated as templates with a. gp file of the same name. Go code file///////////////////////////////// Example.gp//this is an example of using GPG tool forgeneric-programming
This is a example of using GPG to define a auto-lock globalvalue with generic type
It'll be realized to real go code by GPG tool through the. Gpgfile with the same name
Package Exampleimport (
"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 //Instantiate with Int,string,uint64, respectivelyExample.gp;this is exactlly a INI file
; it is used to generate code from. gp File
[INT]
Type_name=int
Value_type=int
Lock_comment= No modification, no lock required [CONST_STR]
Type_name=conststr
Value_type=string
lock_comment=// No modification, no lock required [const_u64]
Type_name=constu64
Value_type=uint64
lock_comment=//
Can get the shape as follows code file/////////////////////////////////example_gpg_int.go//This file is 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: Vipally@gmail.com
Blog Site:http://blog.sina.com.cn/ally2014//this is an example of using GPG tool forgeneric-programming
This is a example of using GPG to define a auto-lock globalvalue with generic type
It'll be realized to real go code by GPG tool through the. Gpgfile with the same name
Package Exampleimport (
"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 is 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: Vipally@gmail.com
Blog Site:http://blog.sina.com.cn/ally2014//this is an example of using GPG tool forgeneric-programming
This is a example of using GPG to define a auto-lock globalvalue with generic type
It'll be realized to real go code by GPG tool through the. Gpgfile with the same name
Package Exampleimport (
"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
//}


This basically satisfies Golang's need for generics without paying an additional price.
the original blog post copyright @Allyvipally @gmail.com please contact the original author for commercial use only for Exchange learning . Reprint Please specify Source: http://blog.sina.com.cn/ally2014
[1] GPG source code Https://github.com/vipally/gpg
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.