[go-nuts] Re: Roadblock for user-implemented JIT compiler

2019-11-02 Thread Max


On Friday, November 1, 2019 at 8:58:13 AM UTC+1, Sokolov Yura wrote:
>
> Don't forget about calling to write barriers. 


Of course.

Second update: calling arbitrary Go functions from ARM64 JIT code works too 
:)

See https://github.com/cosmos72/gomacro/blob/master/jit/_hide_from_stack

-- 
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/a7488bd6-348f-4502-9dff-db26d3dab193%40googlegroups.com.


[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.


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

2019-11-02 Thread 林风
totally agree with not use old java thing in go. but here we are trying to 
solve a real problem. 
i read your code, it make sense to me we need a interface to fib, so i 
write such code (still i don't think your code can work with fib(n) n>1 
since you did not use Recursive, when you use Recursive in fib or anywhere 
you will know why it is hard to implement such thing) 

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

i think we can over this discussion. the solution above is enough to me to 
solve related problem. 

Thanks again to inspire me~

在 2019年11月2日星期六 UTC+8下午11:27:15,burak serdar写道:
>
> You don't need to change the original function. Below, Fib(int) int is 
> the original function, and it illustrates two ways to decorate it. 
>
> https://play.golang.org/p/K6us_rHIsyM 
>
> Neither is a *bad* way to write code. Go is a different language so 
> some things you're used to in other languages must be done in 
> different ways. This sometimes requires you to re-engineer your 
> solution. So trying to replicate a Java decorator pattern 
> implementation in Go using Java features won't work. Change the way 
> you're trying to solve your problem. 
>
> You use decorators to make existing code fit another scenario. For 
> this to work, the receiving end of the problem must be able to handle 
> such decoration as well. If you have code that uses the function `Fib` 
> directly, then you cannot override Fib. You cannot override Fib in 
> Java either (don't know about python). Your code should be able to get 
> an indirection of Fib (either an interface or a function pointer) so 
> you can override it. 
>
>
> On Sat, Nov 2, 2019 at 4:34 AM 林风 > 
> wrote: 
> > 
> > there is a way i can do the base, but we still change old function due 
> limit on go 
> > 
> > https://gist.github.com/Petelin/a7bf8ccfd657536d46aaa355c6dc421a 
> > 
> > 
> > 在 2019年11月2日星期六 UTC+8下午4:50:22,林风写道: 
> >> 
> >> you may say i can write a fib2 which have fib1+counter. but that is 
> really bad way to write code. and that's why we need decorator. 
> >> 
> >> 在 2019年11月2日星期六 UTC+8下午2:30:33,burak serdar写道: 
> >>> 
> >>> On Sat, Nov 2, 2019 at 12:20 AM 林风  wrote: 
> >>> > 
> >>> > 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? 
> >>> 
> >>> 
> >>> There is no built-in way to do that. I don't think you can do that 
> >>> with reflect either. However, if you need such decorators, you can use 
> >>> an interface or a function pointer: 
> >>> 
> >>> type fib interface { 
> >>>fib(int) int 
> >>> } 
> >>> 
> >>> func f(in fib) { 
> >>>in.fib(...) 
> >>> } 
> >>> 
> >>> You can pass in an implementation of fib that will do whatever you 
> want. 
> >>> 
> >>> With a function pointer: 
> >>> 
> >>> func f(fib func(int) int) { 
> >>>fib(...) 
> >>> } 
> >>> 
> >>> And if you need to count how many times f calls fib: 
> >>> 
> >>> ... 
> >>> count:=0 
> >>> f(func(in int) int { 
> >>>count++ 
> >>>return originalFib(in) 
> >>>   } 
> >>> } 
> >>> 
> >>> 
> >>> 
> >>> > 
> >>> > 
> >>> > 
> >>> > 
> >>> > 
> >>> > 
> >>> > -- 
> >>> > 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 golan...@googlegroups.com. 
> >>> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>  
>
> > 
> > -- 
> > 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 golan...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/ad4658a5-c6a6-4cfb-ad48-95ce8ecbfa2b%40googlegroups.com.
>  
>
>

-- 
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/ec2cdafb-e9d0-4bb5-bbf0-3f29e51a57c3%40googlegroups.com.


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

2019-11-02 Thread burak serdar
You don't need to change the original function. Below, Fib(int) int is
the original function, and it illustrates two ways to decorate it.

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

Neither is a *bad* way to write code. Go is a different language so
some things you're used to in other languages must be done in
different ways. This sometimes requires you to re-engineer your
solution. So trying to replicate a Java decorator pattern
implementation in Go using Java features won't work. Change the way
you're trying to solve your problem.

You use decorators to make existing code fit another scenario. For
this to work, the receiving end of the problem must be able to handle
such decoration as well. If you have code that uses the function `Fib`
directly, then you cannot override Fib. You cannot override Fib in
Java either (don't know about python). Your code should be able to get
an indirection of Fib (either an interface or a function pointer) so
you can override it.


On Sat, Nov 2, 2019 at 4:34 AM 林风  wrote:
>
> there is a way i can do the base, but we still change old function due limit 
> on go
>
> https://gist.github.com/Petelin/a7bf8ccfd657536d46aaa355c6dc421a
>
>
> 在 2019年11月2日星期六 UTC+8下午4:50:22,林风写道:
>>
>> you may say i can write a fib2 which have fib1+counter. but that is really 
>> bad way to write code. and that's why we need decorator.
>>
>> 在 2019年11月2日星期六 UTC+8下午2:30:33,burak serdar写道:
>>>
>>> On Sat, Nov 2, 2019 at 12:20 AM 林风  wrote:
>>> >
>>> > 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?
>>>
>>>
>>> There is no built-in way to do that. I don't think you can do that
>>> with reflect either. However, if you need such decorators, you can use
>>> an interface or a function pointer:
>>>
>>> type fib interface {
>>>fib(int) int
>>> }
>>>
>>> func f(in fib) {
>>>in.fib(...)
>>> }
>>>
>>> You can pass in an implementation of fib that will do whatever you want.
>>>
>>> With a function pointer:
>>>
>>> func f(fib func(int) int) {
>>>fib(...)
>>> }
>>>
>>> And if you need to count how many times f calls fib:
>>>
>>> ...
>>> count:=0
>>> f(func(in int) int {
>>>count++
>>>return originalFib(in)
>>>   }
>>> }
>>>
>>>
>>>
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > 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 golan...@googlegroups.com.
>>> > To view this discussion on the web visit 
>>> > https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>
> --
> 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/ad4658a5-c6a6-4cfb-ad48-95ce8ecbfa2b%40googlegroups.com.

-- 
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/CAMV2Rqr5uzTL8FKN%3D6NhbZaCrBir27OCMhk-1i8O5MP4nLDfKQ%40mail.gmail.com.


[go-nuts] liteide x36.2 released.

2019-11-02 Thread visualfc
Hi, all.
LiteIDE X36.2 released!
This version fix gocode crash bug. Add new image viewer plugins. Folder view 
support multi copy, move to trash. Fix windows floating dock widget style.

* LiteIDE Home
       
* LiteIDE Source code
       
* Release downloads
       
       

### 2019.10.30 Ver X36.2
* LiteIDE
    * add new image viewer plugin
    * folder view support multi copy & paste
    * folder view support move to trash
    * fix gocode crash
    * update uk (Ukrainian) translation, thanks for cl0ne
* LiteApp
    * fix floating dock widet style
    * add dock widget floating toolbar
    * folder view support multi copy and paste
    * folder view support move to trash action
* ImageEditor
    * add new image viewer plugin
    * support image viewer and gif video play
* GolangEdit
    * support go1.13 number literal syntax highlight
* gotools & gocode
    * fix bad parser crash

Sent from YoMail for Gmail

-- 
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/emc_e99421b82d804a4d8a64233ab621c5aa%40pc.loc.


[go-nuts] mcache per P or per M?

2019-11-02 Thread Vincent Blanchon
Hello,

Reading the code, I can see the structs m and p hold a mcache instance. I'm 
curious to understand why both of them need an instance of mcache?
Also, I see that those instances are the same ones (runtime/proc.go init 
the p.mcache with m.cache and vice versa), is it correct? Should we 
consider that mcache belongs to m or p?

Thank you for the ones that will take time to reply :)

-- 
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/bba54eca-6b54-4566-b5c7-933f7ce6f401%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.


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

2019-11-02 Thread 林风
there is a way i can do the base, but we still change old function due 
limit on go

https://gist.github.com/Petelin/a7bf8ccfd657536d46aaa355c6dc421a


在 2019年11月2日星期六 UTC+8下午4:50:22,林风写道:
>
> you may say i can write a fib2 which have fib1+counter. but that is really 
> bad way to write code. and that's why we need decorator. 
>
> 在 2019年11月2日星期六 UTC+8下午2:30:33,burak serdar写道:
>>
>> On Sat, Nov 2, 2019 at 12:20 AM 林风  wrote: 
>> > 
>> > 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? 
>>
>>
>> There is no built-in way to do that. I don't think you can do that 
>> with reflect either. However, if you need such decorators, you can use 
>> an interface or a function pointer: 
>>
>> type fib interface { 
>>fib(int) int 
>> } 
>>
>> func f(in fib) { 
>>in.fib(...) 
>> } 
>>
>> You can pass in an implementation of fib that will do whatever you want. 
>>
>> With a function pointer: 
>>
>> func f(fib func(int) int) { 
>>fib(...) 
>> } 
>>
>> And if you need to count how many times f calls fib: 
>>
>> ... 
>> count:=0 
>> f(func(in int) int { 
>>count++ 
>>return originalFib(in) 
>>   } 
>> } 
>>
>>
>>
>> > 
>> > 
>> > 
>> > 
>> > 
>> > 
>> > -- 
>> > 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 golan...@googlegroups.com. 
>> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>>  
>>
>>
>

-- 
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/ad4658a5-c6a6-4cfb-ad48-95ce8ecbfa2b%40googlegroups.com.


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

2019-11-02 Thread 林风
you may say i can write a fib2 which have fib1+counter. but that is really 
bad way to write code. and that's why we need decorator. 

在 2019年11月2日星期六 UTC+8下午2:30:33,burak serdar写道:
>
> On Sat, Nov 2, 2019 at 12:20 AM 林风 > 
> wrote: 
> > 
> > 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? 
>
>
> There is no built-in way to do that. I don't think you can do that 
> with reflect either. However, if you need such decorators, you can use 
> an interface or a function pointer: 
>
> type fib interface { 
>fib(int) int 
> } 
>
> func f(in fib) { 
>in.fib(...) 
> } 
>
> You can pass in an implementation of fib that will do whatever you want. 
>
> With a function pointer: 
>
> func f(fib func(int) int) { 
>fib(...) 
> } 
>
> And if you need to count how many times f calls fib: 
>
> ... 
> count:=0 
> f(func(in int) int { 
>count++ 
>return originalFib(in) 
>   } 
> } 
>
>
>
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 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 golan...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>  
>
>

-- 
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/ffb47a51-cf8f-47e8-9789-389d291388f2%40googlegroups.com.


Re: [go-nuts] 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:30:33,burak serdar写道:
>
> On Sat, Nov 2, 2019 at 12:20 AM 林风 > 
> wrote: 
> > 
> > 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? 
>
>
> There is no built-in way to do that. I don't think you can do that 
> with reflect either. However, if you need such decorators, you can use 
> an interface or a function pointer: 
>
> type fib interface { 
>fib(int) int 
> } 
>
> func f(in fib) { 
>in.fib(...) 
> } 
>
> You can pass in an implementation of fib that will do whatever you want. 
>
> With a function pointer: 
>
> func f(fib func(int) int) { 
>fib(...) 
> } 
>
> And if you need to count how many times f calls fib: 
>
> ... 
> count:=0 
> f(func(in int) int { 
>count++ 
>return originalFib(in) 
>   } 
> } 
>
>
>
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 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 golan...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>  
>
>

-- 
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/3a0ad2fb-876c-449e-9de1-d0eb29957c85%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.


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

2019-11-02 Thread burak serdar
On Sat, Nov 2, 2019 at 12:20 AM 林风  wrote:
>
> 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?


There is no built-in way to do that. I don't think you can do that
with reflect either. However, if you need such decorators, you can use
an interface or a function pointer:

type fib interface {
   fib(int) int
}

func f(in fib) {
   in.fib(...)
}

You can pass in an implementation of fib that will do whatever you want.

With a function pointer:

func f(fib func(int) int) {
   fib(...)
}

And if you need to count how many times f calls fib:

...
count:=0
f(func(in int) int {
   count++
   return originalFib(in)
  }
}



>
>
>
>
>
>
> --
> 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/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.

-- 
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/CAMV2RqrD6k9LDdQOvydXjPbHKvSZG-igyPQ5m8GYX4mGE1NXPQ%40mail.gmail.com.


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

2019-11-02 Thread 林风
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/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.