[go-nuts] Re: How can i create a counter decorator in go?

2019-11-02 Thread 林风
thanks for the idea, totally works. simple than my interface implement. 

@burak serdar not sure you are talking about this kind of program or not, 
if yes, then sorry for my misunderstanding. it works. 

https://play.golang.org/p/DnSc5vgItsL

在 2019年11月2日星期六 UTC+8下午7:44:02,Tamás Gulácsi写道:
>
> 2019. november 2., szombat 7:20:02 UTC+1 időpontban 林风 a következőt írta:
>>
>> let's say i have a function or method to get fib
>>
>> func fib(n int){
>>   ...
>>   return fib(n-1) + fib(n-2)
>> }
>>
>>
>> now i want to add a counter to track how many times i have called to fib.
>>
>> in python or jave i can easy use `decorator patten` to do it. 
>>
>> but in go, you can not change fib since it is a function. if you define 
>> fib as a method, you cannot do it either since go did not have 
>> `Inherited`. you cannot call father class.
>>
>> so golang did not support such thing? can we just reflect to do so?  
>>
>>
>>
>>
>>
>>
>> You CAN override the function, but only if it is overridable (exported 
> and changeable):
>
> ```
> var Fib func(n int)
> func init(){
>   Fib = func(n int) {
> ...
> return Fib(n-1) + Fib(n-2)
>   }
> }
> ```
> this way you can override it:
> ```
> ori := pkg.Fib
> pkg.Fib = func(n int) {
>   count++
>   ori(n)
> }
> ```
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/7fe3faaf-a9c0-41aa-b7f0-55757654966d%40googlegroups.com.


[go-nuts] Re: How can i create a counter decorator in go?

2019-11-02 Thread Tamás Gulácsi
2019. november 2., szombat 7:20:02 UTC+1 időpontban 林风 a következőt írta:
>
> let's say i have a function or method to get fib
>
> func fib(n int){
>   ...
>   return fib(n-1) + fib(n-2)
> }
>
>
> now i want to add a counter to track how many times i have called to fib.
>
> in python or jave i can easy use `decorator patten` to do it. 
>
> but in go, you can not change fib since it is a function. if you define 
> fib as a method, you cannot do it either since go did not have `Inherited`
> . you cannot call father class.
>
> so golang did not support such thing? can we just reflect to do so?  
>
>
>
>
>
>
> You CAN override the function, but only if it is overridable (exported and 
changeable):

```
var Fib func(n int)
func init(){
  Fib = func(n int) {
...
return Fib(n-1) + Fib(n-2)
  }
}
```
this way you can override it:
```
ori := pkg.Fib
pkg.Fib = func(n int) {
  count++
  ori(n)
}
```

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/aa374615-6960-494c-8404-a7e7cd5b9943%40googlegroups.com.


[go-nuts] Re: How can i create a counter decorator in go?

2019-11-02 Thread 林风
Hi, thanks for your answer, and i really cannot understander your code. can 
you write a more clear version in go play ground? 
i already create a template. pls notice that we cannot change origin fib 
function.
https://play.golang.org/p/hYerZ2fv0dT

在 2019年11月2日星期六 UTC+8下午2:20:02,林风写道:
>
> let's say i have a function or method to get fib
>
> func fib(n int){
>   ...
>   return fib(n-1) + fib(n-2)
> }
>
>
> now i want to add a counter to track how many times i have called to fib.
>
> in python or jave i can easy use `decorator patten` to do it. 
>
> but in go, you can not change fib since it is a function. if you define 
> fib as a method, you cannot do it either since go did not have `Inherited`
> . you cannot call father class.
>
> so golang did not support such thing? can we just reflect to do so?  
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c4fbe653-3040-403d-988d-be2d92dcf509%40googlegroups.com.