It sounds like maybe this is your first statically typed language. Go is 
statically typed, which means that the scenarios you are talking about can 
not happen. That is by design, and has huge benefits for code reliability, 
and also readability. If you call a function that takes a Foo struct as a 
parameter, then it is impossible for that function to even try to access 
non-existent fields or members. That would be a  compile time error. So the 
'problem' you are trying to solve is already solved, insofar as it can 
never occur.  (It might be, I suppose, possible to 'hack' such a case using 
unsafe, but you should never see or write such code.)

Working in a statically typed language takes some getting used to, and can 
feel restrictive at first. But I almost always prefer them for production 
level code. Remember simplicity, readability (which includes 
comprehensibility) and reliability are cornerstones of the Go language. 

Good Luck, and enjoy learning Go. 

On Sunday, April 12, 2020 at 12:20:48 AM UTC-4, Tanmay Das wrote:
>
> 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> 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/035f4ce2-9b1f-40ae-8c33-52de6a677220%40googlegroups.com.

Reply via email to