Press "Enter" to skip to content

Pointer Receivers – Go Lang Practical Programming Tutorial p.8


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
Please follow and like us:

Be First to Comment

Leave a Reply