No video

Mastering Inheritance By Struct Embedding In Golang

  Рет қаралды 29,753

Anthony GG

Anthony GG

Жыл бұрын

► Join my Discord community for free education 👉 / discord
► Become a Patreon for more exclusive content 👉 / anthonygg_
► Buy my a coffee 👉 donate.stripe....
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
In this Golang tutorial, I'm going to teach you everything you need to know about inheritance by struct embedding and composition in Golang.
#golang

Пікірлер: 88
@anthonygg_
@anthonygg_ Жыл бұрын
► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz ► Become a Patreon for exclusive tutorials👉 www.patreon.com/anthonygg_ ► Buy me a coffee 👉 donate.stripe.com/aEU2a6ayH2uCa3u4gg Thanks for watching
@iwolfman37
@iwolfman37 Жыл бұрын
Yo, why are you like the only person doing content like this on Go? There needs to be more of this
@anthonygg_
@anthonygg_ Жыл бұрын
Send everyone here 🎉
@sephirothu1290
@sephirothu1290 6 ай бұрын
This man look's like a person who can get things done .
@anthonygg_
@anthonygg_ 6 ай бұрын
Sometimes 😂
@xxxPrzybyLxxx
@xxxPrzybyLxxx 11 ай бұрын
I would argue that You don't actually need the *Position to be embedded by-pointer here, simply Position embedded by-value would be sufficient. Unless You want Enemy and Player share the SAME Position structure at some point -- maybe they have merged together in time-and-space?
@jondoe79
@jondoe79 Жыл бұрын
You will definitely become "Elite GO" with Anthony 👍
@user-ge7sw7yr2o
@user-ge7sw7yr2o Жыл бұрын
Time well spent. Thanks Anthony
@Dsouza10082
@Dsouza10082 8 ай бұрын
Thank you !! I have ADHD and your content is dopamina producer.
@oscarllamas
@oscarllamas Жыл бұрын
Very interesting, I was expecting something like player.Position.Move(), but all clear about struct embedding. Cool! Cheers from Central America, Guatemala
@artur9732
@artur9732 Жыл бұрын
Best movie for Friday night)
@BabaykaMoscow
@BabaykaMoscow Жыл бұрын
Why do you use pointer to Position inside Player but non-pointer in the SpecialPosition?
@yaminokaze4475
@yaminokaze4475 Жыл бұрын
Same Question, If you found an answer plz let me know.
@alexobzor
@alexobzor Жыл бұрын
​@@yaminokaze4475 i also want to know this, but i think, he just made a mistake, because there is no reason to make it non pointer.
@alexobzor
@alexobzor Жыл бұрын
@@yaminokaze4475 I found out, that if we use pointer, we should somewhere allocate a memory for the pointer like &Position{}. So, we will have to write a contructor for a SpecialPosition struct, if we embbed Poisition in it like a pointer, it will be like this: func NewSpecialPosition() *SpecialPosition { return &SpecialPosition{ Position: &Position{}, } }
@rijulranjan8514
@rijulranjan8514 Жыл бұрын
@@alexobzorThanks, this really helped!
@anibaldk
@anibaldk 6 ай бұрын
I find go a bit inconsistent. You can embed with and without the * (position is embedded into SpecialPosition NOT as a pointer) and works the same.
@yotelolailo
@yotelolailo Ай бұрын
Shouldn't be the case that the pointer version stores a pointer to the Position struct and the other ones stores a full copy by value of the struct? It's not the same.
@aggellos2001
@aggellos2001 Жыл бұрын
Very informative. Great work 👍
@Huboh
@Huboh 3 ай бұрын
Nice! The word you’re looking for is ‘promotion’. Embedding a type within a struct ‘promotes’ fields and method declared on the type to the containing struct. You can even embed an interface within another interface - you can even embed an interface within a struct
@simonced
@simonced Жыл бұрын
Great tutorial, I am learning Go and this is really helpfull. I have one question though. For the type SecialPosition, you added Position (and not *Position) unlike what you did for Player or Ennemi. What is the difference?
@jamesburgess9101
@jamesburgess9101 6 ай бұрын
I had this exact question too
@noone-gz4pc
@noone-gz4pc 5 ай бұрын
Same here
@all-stars-short
@all-stars-short Жыл бұрын
Sweet thanks for this great lesson of Golang
@igor5379
@igor5379 3 ай бұрын
so many points of view: inheritance or embedding)
@auniquehandle
@auniquehandle Жыл бұрын
GG very nice tutorial. First time i can follow all through and understand all haha
@anthonygg_
@anthonygg_ Жыл бұрын
Thanks!
@jacksontaylor3220
@jacksontaylor3220 Жыл бұрын
You are a god. thank you for your content 🙏🏼
@jed271
@jed271 Жыл бұрын
great content as always
@anonymouse1344
@anonymouse1344 Жыл бұрын
Love your videos.Could you please make a video on how to implement port forwarding in golang.
@anthonygg_
@anthonygg_ Жыл бұрын
Noted that on the list
@Jam-ht2ky
@Jam-ht2ky Жыл бұрын
Thank you really clear explanation
@larrasket
@larrasket Жыл бұрын
Great as usual
@iferca
@iferca 10 ай бұрын
Great one ❤
@mahmoudabdelsattar8860
@mahmoudabdelsattar8860 Жыл бұрын
we need problem solving vids on Friday or any day of the week if u have time
@davidbaker7940
@davidbaker7940 Жыл бұрын
Great video, thank you again!
@tpotjj2979
@tpotjj2979 Жыл бұрын
Keep the dopamine coming!
@mahmoudabdelsattar8860
@mahmoudabdelsattar8860 Жыл бұрын
best of the best
@johnlimusicofficial220
@johnlimusicofficial220 6 ай бұрын
Yo man this is so DOPE! I like this approach so much. By the way what is this VScode theme? Looks so warm :)
@anthonygg_
@anthonygg_ 6 ай бұрын
Gruvbox
@johnlimusicofficial220
@johnlimusicofficial220 6 ай бұрын
@@anthonygg_ ahh yes, exactly! Thank you. :)
@pyronb
@pyronb 9 ай бұрын
I noticed that you always make a constructor function to instanciate structures. Would you say that's a good habit to take on ?
@shpluk
@shpluk 7 ай бұрын
Yes
@vcool
@vcool 10 ай бұрын
Pronunciation: "struct" (with u), not "strict" (with i).
@anthonygg_
@anthonygg_ 10 ай бұрын
Ok buddy.
@b3owu1f
@b3owu1f 11 ай бұрын
The ONE thing you didnt cover that's important is what happens if Position and SpecialPosition have the same method names. No overloading capability, so which one is called?
@maximus1172
@maximus1172 7 ай бұрын
the containing type's method is given preference, else you can do "Outer.Inner.field" to select the embedded field or method
@diamantisufi7938
@diamantisufi7938 Жыл бұрын
Big fan!
@anthonygg_
@anthonygg_ Жыл бұрын
My man! How are you?
@diamantisufi7938
@diamantisufi7938 Жыл бұрын
I am doing amazing! Great to see the channel grow like this❤️
@abuk95
@abuk95 8 ай бұрын
That's weird. The "Position" is basically polluting namespace of the "Player".
@JoeyJurjens
@JoeyJurjens Жыл бұрын
Why in the enemy and player struct you embed it as *Position and in the SpecialPostion without the “*”? What’s the difference? 🤔
@anthonygg_
@anthonygg_ Жыл бұрын
Not sure, no particular reason I guess 🥲
@JoeyJurjens
@JoeyJurjens Жыл бұрын
@@anthonygg_ 😅
@zthreee
@zthreee Жыл бұрын
Is there any special reason, why Position that is embedded in SpecialPosition is not a pointer?
@anthonygg_
@anthonygg_ Жыл бұрын
No special reason. You can drop the pointer
@zthreee
@zthreee Жыл бұрын
@@anthonygg_ Cool! Is the struct by default a pointer in that case? Wondering why in Player has a pointer to Position - can we drop it here too? I mean, i get the difference between pointer/not pointer, but wondering that is the proper way to do it in GoLang
@puttenicole
@puttenicole Жыл бұрын
Great work! Wanna buy you a coffee but the link provided(stripe?) is not working for me(get 404).
@anthonygg_
@anthonygg_ Жыл бұрын
God dammit :). For some reason when copy-pasting from another video that link gets truncated somehow. Anyway, normally should be fixed now. Thanks for letting me know, and thanks upfront for the coffee
@puttenicole
@puttenicole Жыл бұрын
@@anthonygg_ Think I had the same problem in some other videos of you. Anyway, above link works now :)
@PiotrTajchman
@PiotrTajchman Жыл бұрын
What's the difference between embedding with a dereferencing '*' and embedding without it?
@verafice
@verafice 11 ай бұрын
Quite a lot of nuance between them! The default value of a reference type is NIL, so if you forget to use the factory method and just build a Player raw with p := Player{} (or forget to init your embedded types in the constructor), it'll have a nil Position. And as you might guess, in this case if you try invoking Player.Move you'll get a runtime error. - so why would I ever want to use a by-reference embed? To keep working with the videogame example, you'll eventually add a Sprite struct to the project. Your Sprite will have some graphics, a physical size, a count of the available frames of animation, maybe it has a draw method or a non-member function handles that, whatever. A lot of enemies will share the same sprite asset, so embedding the Sprite struct by-reference makes sense here. A lot of unique enemy structs can point to the same single instance of Sprite. We can just have it as a reference passed in to the constructor. We'll let each enemy track which frame of animation they are on independently, not within the Sprite struct. Another idea is this opens the door to data-oriented design. let's say you declare all your positions in a big slice. enemies:=make([]Position, 1000) now in your enemy constructor, you point each enemy's Position to its own &enemies[i]. now when you write a function that iterates over "the position of the enemies" it has the advantage of cpu cache coherency. You'll also need to track which positions in the slice are actually 'real', too, fortunately this is a trivial to do, define a new type with by-value Position embedding and an extra bool. by-value has the advantage of keeping things simple. no nil pointer to trip over, no extra malloc or reference for the gc. Should probably be your default go-to, then when you see an embedded pointer you'll think "okay, maybe this is a many-to-few relationship, or there's a special purpose for this."
@PiotrTajchman
@PiotrTajchman 10 ай бұрын
Thank you, makes a lot of sense!
@kruczek1986
@kruczek1986 Жыл бұрын
hi, why are you embedding the pointers instead of values ?
@anthonygg_
@anthonygg_ Жыл бұрын
No real reason better not use them in that case
@pyronb
@pyronb 9 ай бұрын
I noticed that you always make every function public by default, starting with a capitilized letter. Can you please explain that habbit ? Wouldn't it more secure to make all functions private by default ?
@lucascurual9021
@lucascurual9021 6 ай бұрын
What vscode theme is?
@danielmdev
@danielmdev 5 ай бұрын
Is that inheritance ? I would think of it more as Composition, it doesn't inherit anything
@choleralul
@choleralul Жыл бұрын
top gg
@meida_oficial
@meida_oficial Ай бұрын
That is more ECS than OOP
@IvanArdillo
@IvanArdillo 11 ай бұрын
it this Irish or Scottsih accent? I need to know this
@Rundik
@Rundik 8 ай бұрын
Cool workaround, but it't not inheritance
@anthonygg_
@anthonygg_ 8 ай бұрын
Correct but it was such a good keyword for the title
@kallekula84
@kallekula84 Жыл бұрын
Brother, you're providing great content, but please, slow down when typing it gets very hard to follow when you're mistyping every other word.
@anthonygg_
@anthonygg_ Жыл бұрын
You have a point
@asitabhpaul
@asitabhpaul 5 ай бұрын
Bcoz we only know to copy ... 😂
@parlor3115
@parlor3115 Жыл бұрын
That's not inheritance. That's composition and it blows
@anthonygg_
@anthonygg_ Жыл бұрын
Its has its purpose sometimes. But most of the time its not the best fit. So I agree here with you. Inheritance is a click bait.
@parlor3115
@parlor3115 Жыл бұрын
@@anthonygg_ I meant GO's struct embedding. It blows
@thoriqadillah7780
@thoriqadillah7780 Жыл бұрын
why your cursor and your typing is weird?
@anthonygg_
@anthonygg_ Жыл бұрын
Cause Im a weirdo.
@mckhalahan
@mckhalahan Жыл бұрын
It is Vim
@Combatwhombat
@Combatwhombat Жыл бұрын
You put out great content... But man... Your typing style and the clack of your keyboard is distracting. Somehow you stuff a whole bunch of useless extra keystrokes in an effort to go fast. Relax! Fast is smooth and smooth is fast.
@anthonygg_
@anthonygg_ Жыл бұрын
Its more of a tick I do when thinking. Im working on it.
@Combatwhombat
@Combatwhombat Жыл бұрын
@@anthonygg_ have you considered adding some low level background music to obscure the keyboard?
@anthonygg_
@anthonygg_ Жыл бұрын
@@Combatwhombat Im considering adding more keyboard noise in the future. What do you think?
@Combatwhombat
@Combatwhombat Жыл бұрын
@@anthonygg_ I think, I have already stopped watching and googled the topic on a few of your videos as a result. To my knowledge, I don't have misophonia and generally tolerate things that those with it don't. I mean this as constructive feedback. A simple and low background beat track that doesn't overpower or conflict with your voice but creates something more like a white-noise mask is a simple thing to do that doesn't require you "work on it"; it let's you use the keyboard you like, avoids needing excessive filters and work on the audio track, and helps feature your content without drawing attention away.
@Cientaschentuecher
@Cientaschentuecher 29 күн бұрын
*Slow is smooth and smooth is fast :)
How And When To Use io.MultiWriter In Golang!?
12:08
Anthony GG
Рет қаралды 5 М.
This Will Make Everyone Understand Golang Interfaces
21:03
Anthony GG
Рет қаралды 48 М.
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 34 МЛН
Joker can't swim!#joker #shorts
00:46
Untitled Joker
Рет қаралды 35 МЛН
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 30 МЛН
How To Use Goroutines For Aggregating Data In Golang?!
17:15
Anthony GG
Рет қаралды 37 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 103 М.
Golang pointers explained, once and for all
13:49
JamieGo
Рет қаралды 2,9 М.
A Practical Example How To Use Interfaces In Golang
14:42
Anthony GG
Рет қаралды 22 М.
This Is The BEST Way To Structure Your GO Projects
11:08
Melkey
Рет қаралды 70 М.
Important Tips On How To Write Idiomatic Code In Golang
21:52
Anthony GG
Рет қаралды 22 М.
Master Golang with Interfaces
21:54
Kantan Coding
Рет қаралды 10 М.
Golang Error Handling Is Better Than You Think!
18:53
Anthony GG
Рет қаралды 27 М.
Mastering Dependency Injection In Golang
14:29
Anthony GG
Рет қаралды 46 М.
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 34 МЛН