The Power Of Struct Embedding And Interfaces In Golang

Sdílet
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
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Komentáře • 26

  • @anthonygg_
    @anthonygg_  Před 9 měsíci +1

    ► 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

  • @anshkumargarhewal8582
    @anshkumargarhewal8582 Před 9 měsíci +1

    Another very informative video thanks anthony and congrats for 20k Subs.🎉

  • @_Kntch
    @_Kntch Před 9 měsíci +1

    Great video as always 💪🏽💪🏽🔥🔥

  • @stephanschmidt2334
    @stephanschmidt2334 Před 7 měsíci +1

    Love your videos

  • @bl2575
    @bl2575 Před 9 měsíci +5

    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...

    • @Jarek.
      @Jarek. Před 5 měsíci +2

      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 👍

    • @susiebaka3388
      @susiebaka3388 Před 5 měsíci

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

  • @ForeverZer0
    @ForeverZer0 Před 9 měsíci +2

    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?

    • @anthonygg_
      @anthonygg_  Před 9 měsíci +3

      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.

    • @ForeverZer0
      @ForeverZer0 Před 9 měsíci +1

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

  • @kanfit
    @kanfit Před 9 měsíci +1

    hei Anthony, I am just wondering what design parttern should be called for this enbedded struct and interface?

    • @xseman
      @xseman Před 2 měsíci +1

      I'd say it's just polymorphism, not a specific pattern, just a specific implementation

  • @joshi1q2w3e
    @joshi1q2w3e Před 9 měsíci +2

    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.

    • @functionator
      @functionator Před 9 měsíci +2

      Think of them as dictionary objects instead. Embedding is similar to composing dictionaries so you can reference the embedded attributes directly

    • @anthonygg_
      @anthonygg_  Před 9 měsíci +1

      Correct.

    • @joshi1q2w3e
      @joshi1q2w3e Před 9 měsíci +1

      @@anthonygg_ just to clarify were you saying correct to my statement or to @functionator?

    • @anthonygg_
      @anthonygg_  Před 9 měsíci +1

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

    • @jaysaha1967
      @jaysaha1967 Před 9 měsíci

      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

  • @couragic
    @couragic Před 9 měsíci +1

    8:06 Why do you pronounce TileWalker as “TileWalkesh” ? Just curious

  • @xseman
    @xseman Před 2 měsíci +1

    some diagram visualization would help to understand this example

  • @MotivationVideos1337
    @MotivationVideos1337 Před 9 měsíci +2

    Your discord link is dead

  • @Anonymous-js9vc
    @Anonymous-js9vc Před 5 měsíci

    what vscode theme is this ??