Pointer Receivers - Go Lang Practical Programming Tutorial p.8
Skills:
AI Pair Programming70%
Key Takeaways
Explains pointer receivers in Go programming language, including modifying objects with methods
Full Transcript
what is going on everybody welcome to part eight of the go programming tutorial series in this part what we're me talking about is building on top of these methods and in the previous tutorial we talked about the value receivers and now we're going to be talking about pointer receiver methods so the big difference here is that with a pointer receiver we can actually start modifying the actual values the attributes the things in that actual you know struct type basically are they in our case this this car type what if we wanted to modify a value inside of here we can't do that with a valued receiver so that's what we're gonna be doing here to get started the first thing that we're gonna go ahead and do is basically we'll just let's just add it down here so let's say what we want to do is change the top speed of the vehicle so in order to do that we're gonna start with func and then same as before right you're gonna pass seeing car but the big difference here is you're gonna be using a pointer right you're gonna be pointing through there reading through that car type basically so now what we're gonna call this is new top speed and the new top speed is modifying something so we don't actually need to return anything but we do where our going to take parameters which would be the new speed which will just say is it float 64 so now within here all we're gonna really need to do is see top speed km/h equals new speed so now we're gonna go ahead and do is come down here and we're just gonna copy this and then what we'll do is we'll just say a a underscore car dot new top speed and we'll say now the new top speed is 500 kilometers per hour and then now let's repay Staudt km/h miles per hour kilometers per hour miles better just go ahead save that we'll come up here and go run go to go and now we can see it's been modified 495 308 as opposed to yeah that's 23 138 so looks like that worked awesome now the next thing I want to just kind of talk about to really bring this point home is like what's say in which one runs first kilometers per hour so in kilometers per hour what would happen if we said see dots top speed kilometers per hour equals 500 so we can see these are this the real we see these are the starting values so let's just go ahead and run it one more time and we can see that the kilometers per hour sure enough is is the faster value but then when we get back to miles per hour its back to being the original and then this is when it was permanently set because we use the pointer so now what would happen though if we converted this one to a pointer receiver so what would we need to do to convert this method here to be a pointer receiver rather than a value receiver unfortunately it's super super complicated because we basically just need to do this so now we'll go up here if we run this and now we can see that actually these two sets of values are identical because we actually because this is a pointer receiver it's modifying the value really it's doing the exact same thing that we were doing right here right so this has modified the value for kilometers not only kilometers per hour but also miles per hour because kilometers per hour is called before miles per hour obviously if we were to make miles per hour a pointer receiver and not kilometers per hour that that wouldn't happen right kilometers per hour would still wait don't do this to me [Music] okay I was about to just I was about to quit okay so the reason why that worked there it was because of this so let me let me get rid of that but also I'm a little concerned oh and we didn't we didn't modify the speed I think I might be almost done recording tutorials for the day if I'm making that kind of a mistake anyway let's run out one more time so here you can see obviously km/h was unaffected yet but miles per hour is affected as we should have expected and then kilometers per hour and so on so at this point you I think a reasonable person is probably wondering why why would I ever use a value to receiver right like what would be the point of a value receiver if a pointer receiver can do the job of a value receiver and a pointer receiver what's the point well I'm glad you asked so in general so what's going on with a value receiver is it's making cut it's pretty basically making a copy of whatever you're passing so it makes a copy and then so you could do all kinds of stuff to it and you can rest assured that whatever you do is not gonna blow anything up whereas the point of receiver it's actually gonna modify that thing itself so with a value receiver if the struct is small kind of like our struct is very small there's not too many inputs here a value receiver is going to be a better option in terms of efficiency but if the struct is very large because this is making a copy the Pointer receiver is going to be a more efficient operation it's just going to have less garbage associated with it so it really just kind of depends on your your use case now if you go to the text-based version of this tutorial I'll link to a I can actually even just bring it up from the golang Doc's why like should I use here we go should I define methods on values or pointers and this is basically the exact questions so you know they're saying yeah the consideration of efficiency if the receivers you know very large a big struct it would be much cheaper to use a pointer receiver because you're not making that copy but then this I hear about consistency like if them if some of the methods of the type must have a pointer receiver ie it you know needs to modify something then the rest should too to me that makes note I mean that's like silly why why like that doesn't make any sense I mean people are people aren't writing go for its beauty or for like it's super simplicity so people are gonna be using go for efficiency so you should be doing everything for efficiency I think so I'm gonna go ahead and disagree with you there golang Docs but um but but yeah I mean I what I would think it would be the best idea is to write your code and then if you've got examples where it could be a pointer it could be a value receiver convert them to value receivers see if you made huge gains and if you don't make gains that you care about okay fine make them all pointer receivers but to make everything to make everything a pointer receiver just to appease consistency it seems kind of silly for a language like go so actually that's all I want to talk about with pointers and value receivers so the class is over you're free to leave but I do just my show real quickly a function that's gonna do basically the exact same thing just because I suspect some people might be curious and at this point you really should be able to write a function in golang that's going to do what these methods are doing but if you want to see me do it we can go ahead and do that now so like let's say rather than using a pointer or a value receiver what if you how would you write a function that could do basically the exact same thing so for example let's just call func newer underscore top speed and this is gonna take a few things this can take C of type car and then it's gonna take a new speed which will be a float 64 then it's going to return a car type and then finally we can do stuff so what we could say is C dot top speed km/h equals speed and then we can return speed and then what we could do is come down here and rather than a card dot new top speed we can just redefine a car so we could say a car a car equals newer top speed takes the parameter of a car and five hundred did we pass a car near top speed see car speed is a float64 Returns car clearly I'm just reading this error run cannot use speed as type car in return or Oh stupid alright let's try again right okay so as you can see there we we made our modification basically it looks a little wonky because we probably still left yeah that in there so returning back to the way this code should look there you have it so that's a way you could you could just actually just write a function that does the exact same thing of that method although you're gonna have to write over a car basically but so this is almost certainly not to be as efficient but really just to drive home an example of a function that would serve that purpose so anyways we'll bonus session okay that's all for now if you guys have questions comments concerns whatever feel free to leave them below otherwise I'll see you in the next tutorial where we're getting back to our web development example
Original Description
Welcome to part 8 of the Go programming tutorial series, where we're talking about methods in Go lang.
So far, we've used our car type example, showing how we could calcuate the vehicle's speed using a value receiver method. What if we wanted to actually modify the object with a method? How might we do that? To do this, we use a pointer receiver.
Text tutorials and sample code: https://pythonprogramming.net/go/
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Pair Programming
View skill →
🎓
Tutor Explanation
DeepCamp AI