The Power Of Struct Embedding And Interfaces In Golang
Vložit
- čas přidán 2. 09. 2023
- ► Join my Discord community for free education 👉 / discord
► Exclusive Lessons, Mentorship, And Videos 👉 / anthonygg_
► Enjoy a 50% Discount on My Golang Course 👉 fulltimegodev.com
► Learn how I became a self-taught software engineer 👉fulltimegodev.com/#mystory
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
► Join my Discord community for free education 👉 discord.com/invite/Ac7CWREe58
► Exclusive Lessons, Mentorship, And Videos 👉 www.patreon.com/anthonygg_
► 50% OFF on my Golang course 👉 fulltimegodev.com
Thanks for watching
Another very informative video thanks anthony and congrats for 20k Subs.🎉
Great video as always 💪🏽💪🏽🔥🔥
Love your videos
Ty
My problem with golang implicit interface is how do you figure out what function can be used with a type? The standard library doc (AFAIK) doesn't provide a list of functions that can be used with each struct type, which make it difficult to figure out what you can do with it...
This is actually a very good question. Till now I haven't found any better way than just running an instance of godoc locally (with -index option) and just search there. But happy to hear any better solution. Cheers 👍
@@Jarek. i am a new golang user, do you have a chance to explain a bit more how i can use godoc to accomplish this?
I am still a bit new to Go, coming from a C background, so my apologies if this is a stupid question, but what is the purpose of the embedded "base" struct being a reference instead of a value? Does this alter any of the semantics for how the interfaces or "inherited" (for lack of a better word) functions work?
Otherwise wouldn't this simply increase the chances of a nil reference and fragment memory, leading to more cache misses?
At this point of the example a pointer to Enemy is not needed. But, if Enemy is no pointer and you want to modify the underlying transform, called in another function you will modify a copy. But your point on fragmentation and cache misses is valid.
@@anthonygg_ Ah yes, I did not think of that aspect of it. I have gotten into the habit of always using reference receivers over value ones when writing functions which alleviates that, but obviously is not always possible or the desired behavior for every scenario.
I have noticed there are some "gotchas" with embedded structs. The syntatic sugar makes it easy to view it as inheritance, which can lead to shooting yourself in the foot. My first sincere effort at a Go project required a major overhaul because I wrongfully assumed I could cast the "base" type into the type calling the function. This broke my habit of naming the receiver "self" or "this" lol.
hei Anthony, I am just wondering what design parttern should be called for this enbedded struct and interface?
I'd say it's just polymorphism, not a specific pattern, just a specific implementation
So in this case, are objects like Enemy, Transform, and FireEnemy, like classes in Python?
Coming from Python I’m having a hard time understanding how to write Go code because it doesn’t use Classes.
Think of them as dictionary objects instead. Embedding is similar to composing dictionaries so you can reference the embedded attributes directly
Correct.
@@anthonygg_ just to clarify were you saying correct to my statement or to @functionator?
@@joshi1q2w3e @functionater is kinda right here. You need to think about structs as pieces that hold data. If you embed piece A into piece B. B will have the fields of piece A
Go is a complete procedure language so comparing with OOPS of python is not really wise.. but if you want to then classes of python is what would be replaced by gos struct
8:06 Why do you pronounce TileWalker as “TileWalkesh” ? Just curious
Because I make 30k a month
some diagram visualization would help to understand this example
Your discord link is dead
Thanks
what vscode theme is this ??
found it Gruvbox Glass