Press "Enter" to skip to content

Pointer Receivers – Go Lang Practical Programming Tutorial p.8


what is going on everybody welcome to

what is going on everybody welcome to part eight of the go programming

part eight of the go programming

part eight of the go programming tutorial series in this part what we’re

tutorial series in this part what we’re

tutorial series in this part what we’re me talking about is building on top of

me talking about is building on top of

me talking about is building on top of these methods and in the previous

these methods and in the previous

these methods and in the previous tutorial we talked about the value

tutorial we talked about the value

tutorial we talked about the value receivers and now we’re going to be

receivers and now we’re going to be

receivers and now we’re going to be talking about pointer receiver methods

talking about pointer receiver methods

talking about pointer receiver methods so the big difference here is that with

so the big difference here is that with

so the big difference here is that with a pointer receiver we can actually start

a pointer receiver we can actually start

a pointer receiver we can actually start modifying the actual values the

modifying the actual values the

modifying the actual values the attributes the things in that actual you

attributes the things in that actual you

attributes the things in that actual you know struct type basically are they in

know struct type basically are they in

know struct type basically are they in our case this this car type what if we

our case this this car type what if we

our case this this car type what if we wanted to modify a value inside of here

wanted to modify a value inside of here

wanted to modify a value inside of here we can’t do that with a valued receiver

we can’t do that with a valued receiver

we can’t do that with a valued receiver so that’s what we’re gonna be doing here

so that’s what we’re gonna be doing here

so that’s what we’re gonna be doing here to get started the first thing that

to get started the first thing that

to get started the first thing that we’re gonna go ahead and do is basically

we’re gonna go ahead and do is basically

we’re gonna go ahead and do is basically we’ll just let’s just add it down here

we’ll just let’s just add it down here

we’ll just let’s just add it down here so let’s say what we want to do is

so let’s say what we want to do is

so let’s say what we want to do is change the top speed of the vehicle so

change the top speed of the vehicle so

change the top speed of the vehicle so in order to do that we’re gonna start

in order to do that we’re gonna start

in order to do that we’re gonna start with func and then same as before right

with func and then same as before right

with func and then same as before right you’re gonna pass seeing car but the big

you’re gonna pass seeing car but the big

you’re gonna pass seeing car but the big difference here is you’re gonna be using

difference here is you’re gonna be using

difference here is you’re gonna be using a pointer right you’re gonna be pointing

a pointer right you’re gonna be pointing

a pointer right you’re gonna be pointing through there reading through that car

through there reading through that car

through there reading through that car type basically so now what we’re gonna

type basically so now what we’re gonna

type basically so now what we’re gonna call this is new top speed and the new

call this is new top speed and the new

call this is new top speed and the new top speed is modifying something so we

top speed is modifying something so we

top speed is modifying something so we don’t actually need to return anything

don’t actually need to return anything

don’t actually need to return anything but we do where our going to take

but we do where our going to take

but we do where our going to take parameters which would be the new speed

parameters which would be the new speed

parameters which would be the new speed which will just say is it float 64 so

which will just say is it float 64 so

which will just say is it float 64 so now within here all we’re gonna really

now within here all we’re gonna really

now within here all we’re gonna really need to do is see top speed km/h equals

need to do is see top speed km/h equals

need to do is see top speed km/h equals new speed so now we’re gonna go ahead

new speed so now we’re gonna go ahead

new speed so now we’re gonna go ahead and do is come down here and we’re just

and do is come down here and we’re just

and do is come down here and we’re just gonna copy this and then what we’ll do

gonna copy this and then what we’ll do

gonna copy this and then what we’ll do is we’ll just say a a underscore car dot

is we’ll just say a a underscore car dot

is we’ll just say a a underscore car dot new top speed and we’ll say now the new

new top speed and we’ll say now the new

new top speed and we’ll say now the new top speed is 500 kilometers per hour and

top speed is 500 kilometers per hour and

top speed is 500 kilometers per hour and then now let’s repay Staudt km/h miles

then now let’s repay Staudt km/h miles

then now let’s repay Staudt km/h miles per hour kilometers per hour miles

per hour kilometers per hour miles

per hour kilometers per hour miles better just go ahead save that we’ll

better just go ahead save that we’ll

better just go ahead save that we’ll come up here and go run go to go and now

come up here and go run go to go and now

come up here and go run go to go and now we can see it’s been modified 495 308 as

we can see it’s been modified 495 308 as

we can see it’s been modified 495 308 as opposed to yeah that’s

opposed to yeah that’s

opposed to yeah that’s 23 138 so looks like that worked awesome

23 138 so looks like that worked awesome

23 138 so looks like that worked awesome now the next thing I want to just kind

now the next thing I want to just kind

now the next thing I want to just kind of talk about to really bring this point

of talk about to really bring this point

of talk about to really bring this point home is like what’s say in which one

home is like what’s say in which one

home is like what’s say in which one runs first kilometers per hour so in

runs first kilometers per hour so in

runs first kilometers per hour so in kilometers per hour what would happen if

kilometers per hour what would happen if

kilometers per hour what would happen if we said see dots top speed kilometers

we said see dots top speed kilometers

we said see dots top speed kilometers per hour equals 500 so we can see these

per hour equals 500 so we can see these

per hour equals 500 so we can see these are this the real we see these are the

are this the real we see these are the

are this the real we see these are the starting values so let’s just go ahead

starting values so let’s just go ahead

starting values so let’s just go ahead and run it one more time

and run it one more time

and run it one more time and we can see that the kilometers per

and we can see that the kilometers per

and we can see that the kilometers per hour sure enough is is the faster value

hour sure enough is is the faster value

hour sure enough is is the faster value but then when we get back to miles per

but then when we get back to miles per

but then when we get back to miles per hour its back to being the original and

hour its back to being the original and

hour its back to being the original and then this is when it was permanently set

then this is when it was permanently set

then this is when it was permanently set because we use the pointer so now what

because we use the pointer so now what

because we use the pointer so now what would happen though if we converted this

would happen though if we converted this

would happen though if we converted this one to a pointer receiver so what would

one to a pointer receiver so what would

one to a pointer receiver so what would we need to do to convert this method

we need to do to convert this method

we need to do to convert this method here to be a pointer receiver rather

here to be a pointer receiver rather

here to be a pointer receiver rather than a value receiver

than a value receiver

than a value receiver unfortunately it’s super super

unfortunately it’s super super

unfortunately it’s super super complicated because we basically just

complicated because we basically just

complicated because we basically just need to do this so now we’ll go up here

need to do this so now we’ll go up here

need to do this so now we’ll go up here if we run this and now we can see that

if we run this and now we can see that

if we run this and now we can see that actually these two sets of values are

actually these two sets of values are

actually these two sets of values are identical because we actually because

identical because we actually because

identical because we actually because this is a pointer receiver

this is a pointer receiver

this is a pointer receiver it’s modifying the value really it’s

it’s modifying the value really it’s

it’s modifying the value really it’s doing the exact same thing that we were

doing the exact same thing that we were

doing the exact same thing that we were doing right here right so this has

doing right here right so this has

doing right here right so this has modified the value for kilometers not

modified the value for kilometers not

modified the value for kilometers not only kilometers per hour but also miles

only kilometers per hour but also miles

only kilometers per hour but also miles per hour because kilometers per hour is

per hour because kilometers per hour is

per hour because kilometers per hour is called before miles per hour obviously

called before miles per hour obviously

called before miles per hour obviously if we were to make miles per hour a

if we were to make miles per hour a

if we were to make miles per hour a pointer receiver and not kilometers per

pointer receiver and not kilometers per

pointer receiver and not kilometers per hour that that wouldn’t happen right

hour that that wouldn’t happen right

hour that that wouldn’t happen right kilometers per hour would still wait

kilometers per hour would still wait

kilometers per hour would still wait don’t do this to me

okay I was about to just I was about to

okay I was about to just I was about to quit okay so the reason why that worked

quit okay so the reason why that worked

quit okay so the reason why that worked there it was because of this so let me

there it was because of this so let me

there it was because of this so let me let me get rid of that but also I’m a

let me get rid of that but also I’m a

let me get rid of that but also I’m a little concerned oh and we didn’t we

little concerned oh and we didn’t we

little concerned oh and we didn’t we didn’t modify the speed I think I might

didn’t modify the speed I think I might

didn’t modify the speed I think I might be almost done recording tutorials for

be almost done recording tutorials for

be almost done recording tutorials for the day if I’m making that kind of a

the day if I’m making that kind of a

the day if I’m making that kind of a mistake anyway let’s run out one more

mistake anyway let’s run out one more

mistake anyway let’s run out one more time so here you can see obviously km/h

time so here you can see obviously km/h

time so here you can see obviously km/h was unaffected yet but miles per hour is

was unaffected yet but miles per hour is

was unaffected yet but miles per hour is affected as we should have expected and

affected as we should have expected and

affected as we should have expected and then kilometers per hour and so on so at

then kilometers per hour and so on so at

then kilometers per hour and so on so at this point you I think a reasonable

this point you I think a reasonable

this point you I think a reasonable person is probably wondering why why

person is probably wondering why why

person is probably wondering why why would I ever use a value to receiver

would I ever use a value to receiver

would I ever use a value to receiver right like what would be the point of a

right like what would be the point of a

right like what would be the point of a value receiver if a pointer receiver can

value receiver if a pointer receiver can

value receiver if a pointer receiver can do the job of a value receiver and a

do the job of a value receiver and a

do the job of a value receiver and a pointer receiver what’s the point well

pointer receiver what’s the point well

pointer receiver what’s the point well I’m glad you asked

I’m glad you asked

I’m glad you asked so in general so what’s going on with a

so in general so what’s going on with a

so in general so what’s going on with a value receiver

value receiver

value receiver is it’s making cut it’s pretty basically

is it’s making cut it’s pretty basically

is it’s making cut it’s pretty basically making a copy of whatever you’re passing

making a copy of whatever you’re passing

making a copy of whatever you’re passing so it makes a copy and then so you could

so it makes a copy and then so you could

so it makes a copy and then so you could do all kinds of stuff to it and you can

do all kinds of stuff to it and you can

do all kinds of stuff to it and you can rest assured that whatever you do is not

rest assured that whatever you do is not

rest assured that whatever you do is not gonna blow anything up whereas the point

gonna blow anything up whereas the point

gonna blow anything up whereas the point of receiver it’s actually gonna modify

of receiver it’s actually gonna modify

of receiver it’s actually gonna modify that thing itself so with a value

that thing itself so with a value

that thing itself so with a value receiver if the struct is small kind of

receiver if the struct is small kind of

receiver if the struct is small kind of like our struct is very small there’s

like our struct is very small there’s

like our struct is very small there’s not too many inputs here a value

not too many inputs here a value

not too many inputs here a value receiver is going to be a better option

receiver is going to be a better option

receiver is going to be a better option in terms of efficiency but if the struct

in terms of efficiency but if the struct

in terms of efficiency but if the struct is very large because this is making a

is very large because this is making a

is very large because this is making a copy the Pointer receiver is going to be

copy the Pointer receiver is going to be

copy the Pointer receiver is going to be a more efficient operation it’s just

a more efficient operation it’s just

a more efficient operation it’s just going to have less garbage associated

going to have less garbage associated

going to have less garbage associated with it so it really just kind of

with it so it really just kind of

with it so it really just kind of depends on your your use case now if you

depends on your your use case now if you

depends on your your use case now if you go to the text-based version of this

go to the text-based version of this

go to the text-based version of this tutorial I’ll link to a I can actually

tutorial I’ll link to a I can actually

tutorial I’ll link to a I can actually even just bring it up from the golang

even just bring it up from the golang

even just bring it up from the golang Doc’s why like should I use here we go

Doc’s why like should I use here we go

Doc’s why like should I use here we go should I define methods on values or

should I define methods on values or

should I define methods on values or pointers and this is basically the exact

pointers and this is basically the exact

pointers and this is basically the exact questions so you know they’re saying

questions so you know they’re saying

questions so you know they’re saying yeah the consideration of efficiency if

yeah the consideration of efficiency if

yeah the consideration of efficiency if the receivers you know very large a big

the receivers you know very large a big

the receivers you know very large a big struct it would be much cheaper to use a

struct it would be much cheaper to use a

struct it would be much cheaper to use a pointer receiver because you’re not

pointer receiver because you’re not

pointer receiver because you’re not making that copy but then

making that copy but then

making that copy but then this I hear about consistency like if

this I hear about consistency like if

this I hear about consistency like if them if some of the methods of the type

them if some of the methods of the type

them if some of the methods of the type must have a pointer receiver ie

must have a pointer receiver ie

must have a pointer receiver ie it you know needs to modify something

it you know needs to modify something

it you know needs to modify something then the rest should too

to me that makes note I mean that’s like

to me that makes note I mean that’s like silly why why like that doesn’t make any

silly why why like that doesn’t make any

silly why why like that doesn’t make any sense I mean people are people aren’t

sense I mean people are people aren’t

sense I mean people are people aren’t writing go for its beauty or for like

writing go for its beauty or for like

writing go for its beauty or for like it’s super simplicity so people are

it’s super simplicity so people are

it’s super simplicity so people are gonna be using go for efficiency so you

gonna be using go for efficiency so you

gonna be using go for efficiency so you should be doing everything for

should be doing everything for

should be doing everything for efficiency I think so I’m gonna go ahead

efficiency I think so I’m gonna go ahead

efficiency I think so I’m gonna go ahead and disagree with you there golang Docs

and disagree with you there golang Docs

and disagree with you there golang Docs but um but but yeah I mean I what I

but um but but yeah I mean I what I

but um but but yeah I mean I what I would think it would be the best idea is

would think it would be the best idea is

would think it would be the best idea is to write your code and then if you’ve

to write your code and then if you’ve

to write your code and then if you’ve got examples where it could be a pointer

got examples where it could be a pointer

got examples where it could be a pointer it could be a value receiver convert

it could be a value receiver convert

it could be a value receiver convert them to value receivers see if you made

them to value receivers see if you made

them to value receivers see if you made huge gains and if you don’t make gains

huge gains and if you don’t make gains

huge gains and if you don’t make gains that you care about

that you care about

that you care about okay fine make them all pointer

okay fine make them all pointer

okay fine make them all pointer receivers but to make everything to make

receivers but to make everything to make

receivers but to make everything to make everything a pointer receiver just to

everything a pointer receiver just to

everything a pointer receiver just to appease consistency it seems kind of

appease consistency it seems kind of

appease consistency it seems kind of silly for a language like go so actually

silly for a language like go so actually

silly for a language like go so actually that’s all I want to talk about with

that’s all I want to talk about with

that’s all I want to talk about with pointers and value receivers so the

pointers and value receivers so the

pointers and value receivers so the class is over you’re free to leave but I

class is over you’re free to leave but I

class is over you’re free to leave but I do just my show real quickly a function

do just my show real quickly a function

do just my show real quickly a function that’s gonna do basically the exact same

that’s gonna do basically the exact same

that’s gonna do basically the exact same thing just because I suspect some people

thing just because I suspect some people

thing just because I suspect some people might be curious and at this point you

might be curious and at this point you

might be curious and at this point you really should be able to write a

really should be able to write a

really should be able to write a function in golang that’s going to do

function in golang that’s going to do

function in golang that’s going to do what these methods are doing but if you

what these methods are doing but if you

what these methods are doing but if you want to see me do it

want to see me do it

want to see me do it we can go ahead and do that now so like

we can go ahead and do that now so like

we can go ahead and do that now so like let’s say rather than using a pointer or

let’s say rather than using a pointer or

let’s say rather than using a pointer or a value receiver what if you how would

a value receiver what if you how would

a value receiver what if you how would you write a function that could do

you write a function that could do

you write a function that could do basically the exact same thing so for

basically the exact same thing so for

basically the exact same thing so for example let’s just call func newer

example let’s just call func newer

example let’s just call func newer underscore top speed and this is gonna

underscore top speed and this is gonna

underscore top speed and this is gonna take a few things this can take C of

take a few things this can take C of

take a few things this can take C of type car and then it’s gonna take a new

type car and then it’s gonna take a new

type car and then it’s gonna take a new speed which will be a float 64 then it’s

speed which will be a float 64 then it’s

speed which will be a float 64 then it’s going to return a car type and then

going to return a car type and then

going to return a car type and then finally we can do stuff so what we could

finally we can do stuff so what we could

finally we can do stuff so what we could say is C dot top speed km/h equals speed

say is C dot top speed km/h equals speed

say is C dot top speed km/h equals speed and then we can

and then we can

and then we can return speed and then what we could do

return speed and then what we could do

return speed and then what we could do is come down here and rather than a card

is come down here and rather than a card

is come down here and rather than a card dot new top speed we can just redefine a

dot new top speed we can just redefine a

dot new top speed we can just redefine a car so we could say a car a car equals

car so we could say a car a car equals

car so we could say a car a car equals newer top speed takes the parameter of a

newer top speed takes the parameter of a

newer top speed takes the parameter of a car and five hundred

did we pass a car near top speed see car

did we pass a car near top speed see car speed is a float64 Returns car clearly

speed is a float64 Returns car clearly

speed is a float64 Returns car clearly I’m just reading this error run cannot

I’m just reading this error run cannot

I’m just reading this error run cannot use speed as type car in return or Oh

use speed as type car in return or Oh

use speed as type car in return or Oh stupid alright let’s try again right

stupid alright let’s try again right

stupid alright let’s try again right okay so as you can see there we we made

okay so as you can see there we we made

okay so as you can see there we we made our modification basically it looks a

our modification basically it looks a

our modification basically it looks a little wonky because we probably still

little wonky because we probably still

little wonky because we probably still left yeah that in there so returning

left yeah that in there so returning

left yeah that in there so returning back to the way this code should look

back to the way this code should look

back to the way this code should look there you have it so that’s a way you

there you have it so that’s a way you

there you have it so that’s a way you could you could just actually just write

could you could just actually just write

could you could just actually just write a function that does the exact same

a function that does the exact same

a function that does the exact same thing of that method although you’re

thing of that method although you’re

thing of that method although you’re gonna have to write over a car basically

gonna have to write over a car basically

gonna have to write over a car basically but so this is almost certainly not to

but so this is almost certainly not to

but so this is almost certainly not to be as efficient but really just to drive

be as efficient but really just to drive

be as efficient but really just to drive home an example of a function that would

home an example of a function that would

home an example of a function that would serve that purpose so anyways we’ll

serve that purpose so anyways we’ll

serve that purpose so anyways we’ll bonus session okay that’s all for now if

bonus session okay that’s all for now if

bonus session okay that’s all for now if you guys have questions comments

you guys have questions comments

you guys have questions comments concerns whatever feel free to leave

concerns whatever feel free to leave

concerns whatever feel free to leave them below otherwise I’ll see you in the

them below otherwise I’ll see you in the

them below otherwise I’ll see you in the next tutorial where we’re getting back

next tutorial where we’re getting back

next tutorial where we’re getting back to our web development example

”

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *