How to implement Python functools.wraps equivalent in Go?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to implement Python functools.wraps equivalent in Go?



I know I can wrap functions in Go through returning function, bug how to implement Python functools.wraps equivalent in Go? How to attach attribute to functions in Go? Like the code below in Python.


from functools import wraps

def d(f):
def wrapper(*args):
f(*args)

return wrapper

def d_wraps(f):
@wraps(f)
def wrapper(*args):
f(*args)

return wrapper

@d
def f(a=''):
print a

@d_wraps
def g(a=''):
print a

if __name__ == '__main__':
print 'function name: ', f.__name__
f('abc')

print 'function name: ', g.__name__
g('abc')



d does not change function name, d_wraps changes function name. The result is


d


d_wraps


function name: wrapper
abc
function name: g
abc



I want to use the wrapped function as key at runtime. So I want to keep the function name unchanged after wrapped. How to implement the job that d_wraps does in Go?


d_wraps




1 Answer
1



How to attach attribute to functions in Go?



No, You can not attach attribute to functions in Go.



How to implement the job that d_wraps does in Go?



You can implement another function call your functions, and call the function with the new name.


package main

import "fmt"

func main()
g_warp("abc")


func g(a string)
fmt.Println(a);


func g_warp(a string)
g(a+"_mysuffix");



If you want to change the function content but use the same function name, you can use global function variable:


package main

import (
"fmt"
)

func main()
g = gV2
g("hello")


var g = gV1;

func gV1(a string)
fmt.Println(a)

func gV2(a string)
gV1(a+"_suffix")



If you have a lot of functions to have the same wrap logic, you can pass in the origin function and return the new function:


package main

import (
"fmt"
)

func main()
g = wrap(g)
g("hello")
f = wrap(f)
f("hello")


var g = gV1;

func gV1(a string)
fmt.Println(a)

var f = fV1;

func fV1(a string)
fmt.Println(a+" "+a)

func wrap(originFn func(a string)) func(a string)
return func(a string)
originFn(a+"_suffix")






Thanks. @bronze man. But I think this method works only if I have a few functions. I manually write a new functions for each function. But if I have plenty of functions and I want to use wrappers, I need the function name not changed after wrapped. I want to use the function as key at runtime.
– tangle
Aug 12 at 1:14





If you want to change the function content but use the same function name, you can use global function variable, see my code example in the post.
– bronze man
Aug 12 at 1:19





Thanks. Seems to work if gV2 is simple. But I just check my code, the gV2 is a function with about 30 lines of code, which is a little complex. Not easy to write a gV2 for each function. So I think I still need to use wrappers.
– tangle
Aug 12 at 1:38





If you have a lot of functions to have the same wrap logic, you can pass in the origin function and return the new function, see my code example in the post.
– bronze man
Aug 12 at 1:46






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard