Mastering Inheritance By Struct Embedding In Golang

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

ความคิดเห็น • 91

  • @anthonygg_
    @anthonygg_  2 ปีที่แล้ว +1

    ► 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 ปีที่แล้ว +72

    Yo, why are you like the only person doing content like this on Go? There needs to be more of this

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +17

      Send everyone here 🎉

  • @sephirothu1290
    @sephirothu1290 9 หลายเดือนก่อน +16

    This man look's like a person who can get things done .

    • @anthonygg_
      @anthonygg_  9 หลายเดือนก่อน +3

      Sometimes 😂

  • @DanyalSaleem-z5z
    @DanyalSaleem-z5z ปีที่แล้ว +3

    Time well spent. Thanks Anthony

  • @Deploy-Destroy
    @Deploy-Destroy 3 หลายเดือนก่อน

    I'm an old dog, but I still love learning. Your great videos inspire me.

  • @jondoe79
    @jondoe79 ปีที่แล้ว +6

    You will definitely become "Elite GO" with Anthony 👍

  • @oscarllamas
    @oscarllamas 2 ปีที่แล้ว +5

    Very interesting, I was expecting something like player.Position.Move(), but all clear about struct embedding. Cool! Cheers from Central America, Guatemala

  • @Dsouza10082
    @Dsouza10082 11 หลายเดือนก่อน +2

    Thank you !! I have ADHD and your content is dopamina producer.

  • @artur9732
    @artur9732 2 ปีที่แล้ว +3

    Best movie for Friday night)

  • @LS-jv4uh
    @LS-jv4uh 3 หลายเดือนก่อน

    Every time I think "I'm gonna learn Rust" I am reminded how beautiful and elegant Go is, and I stay put.

  • @aggellos2001
    @aggellos2001 2 ปีที่แล้ว +2

    Very informative. Great work 👍

  • @all-stars-short
    @all-stars-short ปีที่แล้ว +1

    Sweet thanks for this great lesson of Golang

  • @simonced
    @simonced ปีที่แล้ว +11

    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 9 หลายเดือนก่อน +2

      I had this exact question too

    • @noone-gz4pc
      @noone-gz4pc 8 หลายเดือนก่อน +1

      Same here

  • @jacksontaylor3220
    @jacksontaylor3220 ปีที่แล้ว +1

    You are a god. thank you for your content 🙏🏼

  • @Jam-ht2ky
    @Jam-ht2ky ปีที่แล้ว +1

    Thank you really clear explanation

  • @jed271
    @jed271 2 ปีที่แล้ว +1

    great content as always

  • @anibaldk
    @anibaldk 10 หลายเดือนก่อน +4

    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 5 หลายเดือนก่อน

      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.

  • @Huboh
    @Huboh 7 หลายเดือนก่อน

    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

  • @auniquehandle
    @auniquehandle 2 ปีที่แล้ว +3

    GG very nice tutorial. First time i can follow all through and understand all haha

  • @BabaykaMoscow
    @BabaykaMoscow ปีที่แล้ว +8

    Why do you use pointer to Position inside Player but non-pointer in the SpecialPosition?

    • @yaminokaze4475
      @yaminokaze4475 ปีที่แล้ว +3

      Same Question, If you found an answer plz let me know.

    • @alexobzor
      @alexobzor ปีที่แล้ว +1

      ​@@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 ปีที่แล้ว +6

      @@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!

  • @iferca
    @iferca ปีที่แล้ว +1

    Great one ❤

  • @johnlimusicofficial220
    @johnlimusicofficial220 9 หลายเดือนก่อน +1

    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_  9 หลายเดือนก่อน +1

      Gruvbox

    • @johnlimusicofficial220
      @johnlimusicofficial220 9 หลายเดือนก่อน

      @@anthonygg_ ahh yes, exactly! Thank you. :)

  • @igor5379
    @igor5379 7 หลายเดือนก่อน

    so many points of view: inheritance or embedding)

  • @larrasket
    @larrasket ปีที่แล้ว +1

    Great as usual

  • @davidbaker7940
    @davidbaker7940 ปีที่แล้ว

    Great video, thank you again!

  • @mahmoudabdelsattar8860
    @mahmoudabdelsattar8860 ปีที่แล้ว +1

    we need problem solving vids on Friday or any day of the week if u have time

  • @mahmoudabdelsattar8860
    @mahmoudabdelsattar8860 ปีที่แล้ว +1

    best of the best

  • @tpotjj2979
    @tpotjj2979 ปีที่แล้ว +1

    Keep the dopamine coming!

  • @anonymouse1344
    @anonymouse1344 2 ปีที่แล้ว +4

    Love your videos.Could you please make a video on how to implement port forwarding in golang.

    • @anthonygg_
      @anthonygg_  2 ปีที่แล้ว +1

      Noted that on the list

  • @pyronb
    @pyronb ปีที่แล้ว +2

    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 11 หลายเดือนก่อน

      Yes

  • @diamantisufi7938
    @diamantisufi7938 2 ปีที่แล้ว +1

    Big fan!

    • @anthonygg_
      @anthonygg_  2 ปีที่แล้ว

      My man! How are you?

    • @diamantisufi7938
      @diamantisufi7938 2 ปีที่แล้ว

      I am doing amazing! Great to see the channel grow like this❤️

  • @b3owu1f
    @b3owu1f ปีที่แล้ว +2

    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 10 หลายเดือนก่อน

      the containing type's method is given preference, else you can do "Outer.Inner.field" to select the embedded field or method

  • @vcool
    @vcool ปีที่แล้ว +2

    Pronunciation: "struct" (with u), not "strict" (with i).

  • @JoeyJurjens
    @JoeyJurjens ปีที่แล้ว +1

    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_ 😅

  • @abuk95
    @abuk95 11 หลายเดือนก่อน

    That's weird. The "Position" is basically polluting namespace of the "Player".

  • @PiotrTajchman
    @PiotrTajchman ปีที่แล้ว +1

    What's the difference between embedding with a dereferencing '*' and embedding without it?

    • @verafice
      @verafice ปีที่แล้ว +3

      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 ปีที่แล้ว

      Thank you, makes a lot of sense!

  • @kruczek1986
    @kruczek1986 ปีที่แล้ว +1

    hi, why are you embedding the pointers instead of values ?

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +1

      No real reason better not use them in that case

  • @puttenicole
    @puttenicole ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      @@anthonygg_ Think I had the same problem in some other videos of you. Anyway, above link works now :)

  • @zthreee
    @zthreee ปีที่แล้ว +2

    Is there any special reason, why Position that is embedded in SpecialPosition is not a pointer?

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +1

      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

  • @lucascurual9021
    @lucascurual9021 9 หลายเดือนก่อน

    What vscode theme is?

  • @pyronb
    @pyronb ปีที่แล้ว

    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 ?

  • @choleralul
    @choleralul ปีที่แล้ว

    top gg

  • @danielmdev
    @danielmdev 9 หลายเดือนก่อน

    Is that inheritance ? I would think of it more as Composition, it doesn't inherit anything

  • @meida_oficial
    @meida_oficial 5 หลายเดือนก่อน

    That is more ECS than OOP

  • @IvanArdillo
    @IvanArdillo ปีที่แล้ว

    it this Irish or Scottsih accent? I need to know this

  • @Rundik
    @Rundik ปีที่แล้ว +1

    Cool workaround, but it't not inheritance

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว

      Correct but it was such a good keyword for the title

  • @jaberalrafian
    @jaberalrafian 25 วันที่ผ่านมา

    How old are u bro ?

  • @kallekula84
    @kallekula84 ปีที่แล้ว +1

    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.

  • @asitabhpaul
    @asitabhpaul 9 หลายเดือนก่อน

    Bcoz we only know to copy ... 😂

  • @parlor3115
    @parlor3115 ปีที่แล้ว +1

    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?

  • @Combatwhombat
    @Combatwhombat ปีที่แล้ว +1

    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_  ปีที่แล้ว +1

      @@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 4 หลายเดือนก่อน +1

      *Slow is smooth and smooth is fast :)