Mastering Inheritance By Struct Embedding In Golang

Поделиться
HTML-код
  • Опубликовано: 7 ноя 2024

Комментарии • 91

  • @anthonygg_
    @anthonygg_  Год назад +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 Год назад +70

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

  • @sephirothu1290
    @sephirothu1290 9 месяцев назад +15

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

  • @DanyalSaleem-z5z
    @DanyalSaleem-z5z Год назад +3

    Time well spent. Thanks Anthony

  • @Deploy-Destroy
    @Deploy-Destroy 2 месяца назад

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

  • @artur9732
    @artur9732 Год назад +3

    Best movie for Friday night)

  • @oscarllamas
    @oscarllamas Год назад +5

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

  • @jondoe79
    @jondoe79 Год назад +6

    You will definitely become "Elite GO" with Anthony 👍

  • @Dsouza10082
    @Dsouza10082 11 месяцев назад +2

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

  • @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?

  • @Jam-ht2ky
    @Jam-ht2ky Год назад +1

    Thank you really clear explanation

  • @iferca
    @iferca Год назад +1

    Great one ❤

  • @jacksontaylor3220
    @jacksontaylor3220 Год назад +1

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

  • @larrasket
    @larrasket Год назад +1

    Great as usual

  • @all-stars-short
    @all-stars-short Год назад +1

    Sweet thanks for this great lesson of Golang

  • @aggellos2001
    @aggellos2001 Год назад +2

    Very informative. Great work 👍

  • @auniquehandle
    @auniquehandle Год назад +3

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

  • @mahmoudabdelsattar8860
    @mahmoudabdelsattar8860 Год назад +1

    best of the best

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

  • @jed271
    @jed271 Год назад +1

    great content as always

  • @igor5379
    @igor5379 6 месяцев назад

    so many points of view: inheritance or embedding)

  • @davidbaker7940
    @davidbaker7940 Год назад

    Great video, thank you again!

  • @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 :)

  • @anonymouse1344
    @anonymouse1344 Год назад +4

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

  • @anibaldk
    @anibaldk 9 месяцев назад +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 4 месяца назад

      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.

  • @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 10 месяцев назад

      Yes

  • @LS-jv4uh
    @LS-jv4uh 2 месяца назад

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

  • @diamantisufi7938
    @diamantisufi7938 Год назад +1

    Big fan!

    • @anthonygg_
      @anthonygg_  Год назад

      My man! How are you?

    • @diamantisufi7938
      @diamantisufi7938 Год назад

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

  • @mahmoudabdelsattar8860
    @mahmoudabdelsattar8860 Год назад +1

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

  • @tpotjj2979
    @tpotjj2979 Год назад +1

    Keep the dopamine coming!

  • @Huboh
    @Huboh 6 месяцев назад

    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

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

  • @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 :)

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

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

  • @lucascurual9021
    @lucascurual9021 8 месяцев назад

    What vscode theme is?

  • @vcool
    @vcool Год назад +2

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

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

  • @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 ?

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

  • @choleralul
    @choleralul Год назад

    top gg

  • @abuk95
    @abuk95 10 месяцев назад

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

  • @IvanArdillo
    @IvanArdillo Год назад

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

  • @jaberalrafian
    @jaberalrafian 5 дней назад

    How old are u bro ?

  • @danielmdev
    @danielmdev 8 месяцев назад

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

  • @Rundik
    @Rundik 11 месяцев назад +1

    Cool workaround, but it't not inheritance

    • @anthonygg_
      @anthonygg_  11 месяцев назад

      Correct but it was such a good keyword for the title

  • @meida_oficial
    @meida_oficial 4 месяца назад

    That is more ECS than OOP

  • @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 8 месяцев назад

    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 3 месяца назад +1

      *Slow is smooth and smooth is fast :)