Everything You Need To Know About Pointers In Golang

Sdílet
Vložit
  • čas přidán 25. 11. 2022
  • ► Join my Discord community for free education 👉 / discord
    ► Become a Patreon for exclusive tutorials 👉 / anthonygg_
    ► Follow me on Twitter 👉 / anthdm
    ► Follow me on GitHub 👉 github.com/anthdm
    In this Golang tutorial, I will teach you everything you need to know about pointers in Golang. When to use them, and the most common pitfalls new developers will run into.
    #golang

Komentáře • 97

  • @anthonygg_
    @anthonygg_  Před rokem +6

    ► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz
    ► Become a Patreon for exclusive tutorials 👉 www.patreon.com/anthonygg_
    To be precise. A pointer is not really an integer cause it cannot be negative. It is an "address" that points to a specific slot in memory. My excuses for using the word "integer" in this video.
    Thanks for watching

    • @TheDiveO
      @TheDiveO Před rokem

      there are unsigned integers

  • @zoltanhorvath2238
    @zoltanhorvath2238 Před 2 měsíci +4

    It actually shows how intelligent someone is, when they can explain something to you in a very simple way, that's what this guy does. Thanks Anthony!

  • @shamkirnet
    @shamkirnet Před rokem +35

    thanks for the great video!
    a couple of points regarding pointers, perhaps it will be useful to someone.
    1. Pointers allow you to bypass the copying of a variable, what allows you to save memory, as well as change the object by reference without copying it (this was already mentioned in the video)
    2. The pointer value can be nil (var myPointer *int), so it is very convenient to use it, for example, where we return an object from the database, but under certain conditions the object may not be found in the database (the classic example is ORM):
    type UserID int
    type User struct {
    userId UserID
    firstName string
    lastName string
    }
    func FindUser(id UserID) *User {
    var userId UserId = 123
    if id == userId {
    return &User{
    userId: userId
    firstName: "First Name",
    lastName: "Last Name",
    }
    }
    return nil
    }
    func main() {
    if user := FindUser(UserID(123)); user != nil {
    fmt.Println(user.firstName + " " + user.lastName)
    }
    }
    3. Sometimes it is convenient to create a pointer to a particular type with a default value that matches that type. To do this, you can use the "new" function, which returns a pointer to an initialized object in memory with a default value:
    {
    var myPointer *int // myPointer is nil
    }
    {
    myPointer := new(int) // myPointer is an address to an integer type with value 0
    }
    You need to be careful by using pointers, because the uncontrolled use of pointers puts a load on the garbage collector.

    • @shamkirnet
      @shamkirnet Před rokem +5

      one more thing regarding of pointers to reference types (map, chan, slice). it makes no sense to create pointers to them when passing them as an argument to a function in the hope of saving memory, since their structures already contain a pointer, so creating their copies does not lead to large memory costs.

  • @kevinfultz07
    @kevinfultz07 Před rokem +27

    Wish I could hit the like button twice on this! Thanks man.

  • @marckassel8593
    @marckassel8593 Před 3 měsíci +1

    Man that’s a good video, hands on examples are always the key for me to understand something entirely.
    Thanks 🙏🏼

  • @tpotjj2979
    @tpotjj2979 Před rokem +1

    Catching up on some of your videos today, great one again!

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

    Thanks a lot! This video did it for me!
    I understood pointer’s theoretically, but learning them in context is really helpful!
    It also helped me understanding methods in Go, they can be a bit confusing!
    Thanks 😊

  • @sovrinfo
    @sovrinfo Před rokem +2

    Great video. Super big thanks!

  • @definitelynotrohan
    @definitelynotrohan Před rokem +2

    thanks for such detailed explanation!

  • @someshmahajan8414
    @someshmahajan8414 Před rokem +1

    Very informative video
    this helps me to understand the concept very clearly
    Thanks

  • @haoli5986
    @haoli5986 Před 4 měsíci

    thank you so much! this is a very good explainer on a formally very confusing subject in Go for me

  • @harisudarsan211
    @harisudarsan211 Před 11 měsíci +1

    Best youtube channel for golang till now awesome 🎉❤

  • @glengal6490
    @glengal6490 Před rokem +1

    There is joy in just hearing him speak 🥰🥰

  • @jamshidbekyuldoshev7542
    @jamshidbekyuldoshev7542 Před 8 měsíci

    Thanks I really like your explanation about pointers in GO

  • @web3Wizardss
    @web3Wizardss Před rokem +4

    The Goat teacher 🤞

  • @asadsalehumar1011
    @asadsalehumar1011 Před 2 měsíci

    amazing! really helpful👍

  • @nikta456
    @nikta456 Před rokem +1

    Clean cut explanation

  • @denischiosa4496
    @denischiosa4496 Před rokem +1

    good job man, very well explained

  • @caderosche
    @caderosche Před rokem +4

    Wow I've been looking for an explanation like this, being new to a language that heavily uses pointers.

    • @cooldudecs
      @cooldudecs Před rokem +1

      pointers should not be used often unless absolutely necessary. Pass by value makes a lot of sense to limit accidental pointer mutation.

  • @MatheusCabraldosSantos
    @MatheusCabraldosSantos Před rokem +1

    perfect! thank you.

  • @hamzadlm6625
    @hamzadlm6625 Před rokem +1

    Subscribed instantly!

  • @MaciejKoodziejczyk-jq9jo

    awesome video

  • @___DS___
    @___DS___ Před rokem +2

    Mate, thank you. I'm new to golang, coming from python. Even though the concept is clear itself with all them referencing to addresses in memory, the example with DB made the most sense to me lol.
    And offtop:
    I reckon u r Scottish, right? Ur accent made me writing this comment. I absolutely enjoyed it. You r trying so hard to not to swallow half of the letters and sounds they represent lol. So cute. I can't :)) Thank you. I had double pleasure listening to u.
    P.S. Don't consider it offensive or sth, I'm Ukrainian, so for me them things r nothing except amusing and endearing, representing your own culture, which I respect (and enjoyed).

  • @_slier
    @_slier Před rokem +1

    thank you GSP

  • @Nexjsdeveloper
    @Nexjsdeveloper Před rokem +1

    Useful, undrestanble.

  • @TheJurabek
    @TheJurabek Před rokem +1

    thanks mate, how you jump to previous line position after typing something in vim ? like you type “type” and jumped into previous line and typed “Player” and jumped again to previous line😊

  • @atlantic_love
    @atlantic_love Před rokem +3

    Not saying that what you're saying isn't true, but I do find it humorous that on practically every tech-related video out there that host will say "a lot of people are commenting/asking" about (whatever issues is being talked about in video) as a sort of appeal to the masses :D

    • @anthonygg_
      @anthonygg_  Před rokem

      True, I told myself to avoid that as much as possible. But you caught me here.

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

    Im curious what are your thoughts about adding nil checks at the beginning of functions that use pointers or pointer receivers?
    My pov is that it adds "necessary"(i say this loosely) code to your functions as its a time saver when avoiding with panics, although IMO the go complier panic does point you to where the nil deref did happen in the stack trace, so nil checks kind of are nice but not really necessary, just as you said, with great power comes great responsibility. im curious on your stance what i mean if im not clear is
    if fooStructPointer == nil {}

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

      Well. It depends. Only if you know it might be nil, cause it can be nil

  • @pavelzapletal7942
    @pavelzapletal7942 Před 7 měsíci

    Nice example, I am coming from TS world where you have no such pointers and you would just put result from function into a new variable. This is really nice feature in Go.

  • @ThomasSchlosser
    @ThomasSchlosser Před rokem +2

    Thank you for the good, practical lesson about pointers! Two thoughts: 1) Your final recommendation was, only use pointers when you really need it. One reason you explicitly mentioned was „the object is very big“. But the more obvious one might be „some changes need to be made on the object“ - might be a good criteria for those who are unsure. 2) I am not sure, but I imagine that Golang has a „copy on write“ concept (a struct as a parameter is not copied physically unless it is changed - this could be decided roughly at compile time and exactly at runtime). If this would be the case, „chages need not be made“ would be the only case for using a reference (pointer). - I guess a little test programm like your example with the „bigdata“ func coud show, if memory cunsumption grows even if the func has no statement writing on the bigdata-struct. - Or do you already know if Golang has this sort of optimization?

    • @anthonygg_
      @anthonygg_  Před rokem

      To be honest I don't really have a clue :). Like you said, maybe some material for exploration in code.

  • @renypacheco4812
    @renypacheco4812 Před rokem +1

    Thanks for this detailed explanation, i just want to know what vscode theme are you using??

  • @nikydobrev
    @nikydobrev Před rokem +1

    @anthonygg_, can you tell me what VS Code theme you have been using?

  • @RichM1967
    @RichM1967 Před rokem +1

    great video. What I found however is that I was creating a struct for the program's configuration, every function in the program used the struct, so every function had "func (conf *Conf) DoSomething()" -- Instead of doing that, is it bad to just declare conf as a global variable? Are global variables bad?

    • @anthonygg_
      @anthonygg_  Před rokem

      Well depends, globals have there usecase sometimes

  • @luismarcilio
    @luismarcilio Před rokem

    Hey @Antony if you see this comment: I have a doubt: when you pass a pointer, it’s passing the very pointer to the stack as in C language? Or a copy of the pointer that is pointing to the same memory area? Because if it’s the second option, this will require a garbage collection right?

    • @smithshelke2036
      @smithshelke2036 Před 11 měsíci +1

      I think everything is a copy in golang. Same goes for passing pointers

  • @randomness3231
    @randomness3231 Před rokem +2

    Hey @Anthony - do you have a video on how you use VS Code?

    • @anthonygg_
      @anthonygg_  Před rokem +1

      Will make one tomorrow

    • @randomness3231
      @randomness3231 Před rokem +1

      ​@@anthonygg_ wow! thats awesome! I like the way you don't need a mouse. and how you move between tabs, terminal..

  • @nth-prog8562
    @nth-prog8562 Před rokem +2

    Anthony, what theme do u use in vscode?

  • @DavidMwangikamau
    @DavidMwangikamau Před 8 měsíci

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

    GoLang is really confusing in the way how it treats inside the function parameters passed by value - it's always a copy! But then if it's a struct - modifications inside the function won't be visible outside (as your local changes are lost). If it's a slice - you can modify existing elements but if slice capacity changes, it will be lost (as inside the function you have only a copy of slice signature with pointers to the data, length and capacity). Similarly in maps... To get it right, you need deep understanding how data around struct, map, slice is organised and what is actually passed by value.

  • @thiagodias-lo6wn
    @thiagodias-lo6wn Před rokem +1

    Why Go/The IDE does not check the type of Player as nil before letting it execute and return the nil pointer error?

    • @anthonygg_
      @anthonygg_  Před rokem +1

      Hmm good question will take a dive

  • @wuilliam321
    @wuilliam321 Před rokem +1

    I use pointers when nil has a meaning other than the empty struct value. Not so often, but, is it an anti-pattern?

    • @anthonygg_
      @anthonygg_  Před rokem

      I do the same, but its not always the best option. But its easy to compare against

  • @Simple_OG
    @Simple_OG Před měsícem

    As a c/c++ its fell like home 😊

  • @jainilshah7907
    @jainilshah7907 Před rokem +1

    What's the difference between func and methods ?

    • @anthonygg_
      @anthonygg_  Před rokem +1

      Go has no methods. Function receivers are syntactic sugar

  • @k4rshh49
    @k4rshh49 Před rokem +2

    Really don't like this guy's attitude but the content is gold - this was an awesome explanation haha 🤡

    • @anthonygg_
      @anthonygg_  Před rokem

      The king does whatever the fuck he wants. But thanks ❤️

  • @tintin537
    @tintin537 Před rokem +2

    The league of extra ordinary developers!

    • @anthonygg_
      @anthonygg_  Před rokem

      Teemo top

    • @anthonygg_
      @anthonygg_  Před rokem +1

      Or feed

    • @tintin537
      @tintin537 Před rokem

      @@anthonygg_ 😂Fiora top or Jax

    • @anthonygg_
      @anthonygg_  Před rokem +1

      @@tintin537 Be ready. Xmass holidays we gaming LIVE on stream. Do NOT let me down. Hence, carry me.

    • @tintin537
      @tintin537 Před rokem

      @@anthonygg_ I play on eune servers what server do you play on?

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

    never thought I would be learning programming from Johnny Sins himself

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

      I can learn you some other stuff also 😂

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

      @@anthonygg_ LOL, I'll pass this one... Awesome vid btw

  • @ruslangabitov5202
    @ruslangabitov5202 Před rokem +1

    The only thing you should explain is how to DELETE object in Golang. 😅

  • @user-fu2kg4ug2q
    @user-fu2kg4ug2q Před 7 měsíci

    'Who's using a 32-bit machine in 2022' You got a lot of Arch users screaming slurs right now :D

  • @mahmoudabdelsattar8860

    Can you make real project depends on pointers , or give us more pointers examples, specially with big.Int

  • @VyceC
    @VyceC Před rokem

    15:57 it's wrong when you say "it's a function and not a method"

  • @devoverlord
    @devoverlord Před rokem +2

    First

    • @anthonygg_
      @anthonygg_  Před rokem

      You are even faster than my own pinned comment.

  • @demyk214
    @demyk214 Před 10 měsíci

    Man golang intellisense is sloom op windows ik snap wel wrm jij ubuntu gebruik

  • @ayushsrivastava830
    @ayushsrivastava830 Před 11 měsíci +1

    Bro is coding grenade logic for pubg 💀

  • @AlokTripathi
    @AlokTripathi Před rokem +1

    Hi Anthony, If my understanding is correct, here czcams.com/video/mqH21m0MsWk/video.html:
    ```func (player Player) takeDamageFromExplosion() { } ``` is a method ? If not, can you please explain why?

    • @imnishantsharma
      @imnishantsharma Před 11 měsíci +1

      I had same doubt but I think it's a method takeDamageFromExplosion has a receiver of type Player, indicated by (player Player) before the method name.

    • @AlokTripathi
      @AlokTripathi Před 11 měsíci

      @@imnishantsharma that's true! you are right.

  • @sirajul-anik
    @sirajul-anik Před 8 měsíci

    Your videos are good and very much informative. But i would request you to put some effort on typing. It is very much irritating to see all those mistakes throughout each and every videos. If you can't control those typing mistakes, keep it that way. Mechanical kb doesn’t help typing. Sorry if that hurts.

    • @anthonygg_
      @anthonygg_  Před 8 měsíci

      I feel ya. It is indeed very annoying. Even for me. It depends on the day though. I think it gets better in later videos.

  • @josundev
    @josundev Před rokem +1

    I want to hit the like button, but it's on 666 :(

  • @KimberlyWilliamsch
    @KimberlyWilliamsch Před 3 měsíci

    Pointers, learn C instead