Hi, thanks for your reply. I am still in the learning phase and when I 
learn the usage of a new tool I try to keep notes of *what's allowed *and 
*what's 
not allowed* by that tool. That's all :) So at this moment, it's not 
possible for me to come up with a concrete production-level use case. But 
the behavior I described is very common in dynamic languages. If a field 
doesn't exist, create that field at runtime. If a method doesn't exist, 
make some decision on-the-fly, maybe call another method. In both cases, 
the caller *knows* that they (field and method) don't exist and *expects* that 
they will be created/responded accordingly at runtime instead of failing. 
In PHP they're known as *Magic Methods. *In JavaScript, they're called 
*Proxies.* I know that's too much dynamic behavior to ask from a statically 
typed language. But now I know that it's not allowed.

On Sunday, April 12, 2020 at 9:13:03 AM UTC+6, Kurtis Rader wrote:
>
> On Sat, Apr 11, 2020 at 7:59 PM Tanmay Das <tanma...@gmail.com 
> <javascript:>> wrote:
>
>> Say you have a struct Foo and you access fields and call methods on it as 
>> you normally would. But is it possible to execute a hook before or after 
>> that field access or method call? A good scenario will be:
>>
>> The user calls non-existent method foo.Bar() or accesses non-existent 
>> field foo.Bar. If they don't exist, I want to:
>> a) In case of a method call: forward that call to foo.Baz()
>> b) In case of field access: set the foo.Bar at runtime with some value
>>
>
> No, as far as I know. You're looking for a language like Python, which I 
> love, but that isn't the Go model of behavior. This also seems like a 
> http://xyproblem.info/ question. What is it you really want to do? Are 
> you trying to mock something for a unit test?
>
> -- 
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>

-- 
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/4417bc0c-d521-4a3b-a8af-78c399238e80%40googlegroups.com.

Reply via email to