Press "Enter" to skip to content

Types – Go Lang Practical Programming Tutorial p.3


what is going on everybody and welcome

what is going on everybody and welcome to part three of the go tutorial basics

to part three of the go tutorial basics

to part three of the go tutorial basics in this tutorial what were to be talking

in this tutorial what were to be talking

in this tutorial what were to be talking about is typing information like I said

about is typing information like I said

about is typing information like I said in the first tutorial we really are

in the first tutorial we really are

in the first tutorial we really are gonna get to actually applying this to

gonna get to actually applying this to

gonna get to actually applying this to something more useful the thing we’re

something more useful the thing we’re

something more useful the thing we’re gonna apply it to though is go has a

gonna apply it to though is go has a

gonna apply it to though is go has a built-in web server does web obviously

built-in web server does web obviously

built-in web server does web obviously like web applications pretty simply

like web applications pretty simply

like web applications pretty simply right out of the box even with just the

right out of the box even with just the

right out of the box even with just the standard library so that’s what we’re

standard library so that’s what we’re

standard library so that’s what we’re gonna do but even just to do the most

gonna do but even just to do the most

gonna do but even just to do the most basic hello world example we still have

basic hello world example we still have

basic hello world example we still have to get through a lot of like really

to get through a lot of like really

to get through a lot of like really basic truly just basic things about go

basic truly just basic things about go

basic truly just basic things about go especially because I think a lot of

especially because I think a lot of

especially because I think a lot of people are coming from Python since

people are coming from Python since

people are coming from Python since that’s mainly what I do on my channel

that’s mainly what I do on my channel

that’s mainly what I do on my channel and things like today what we’re gonna

and things like today what we’re gonna

and things like today what we’re gonna be talking about is typing information

be talking about is typing information

be talking about is typing information which is something you just don’t even

which is something you just don’t even

which is something you just don’t even have to think about in Python there’s

have to think about in Python there’s

have to think about in Python there’s some little intricacies here that will

some little intricacies here that will

some little intricacies here that will trip you up so anyways let’s go ahead

trip you up so anyways let’s go ahead

trip you up so anyways let’s go ahead and get into it so I’m going to for this

and get into it so I’m going to for this

and get into it so I’m going to for this one we just really need format and we

one we just really need format and we

one we just really need format and we can empty out main and let’s talk about

can empty out main and let’s talk about

can empty out main and let’s talk about types so in go there’s quite a few types

types so in go there’s quite a few types

types so in go there’s quite a few types I’ll put a link in the description to

I’ll put a link in the description to

I’ll put a link in the description to the text-based version of this tutorial

the text-based version of this tutorial

the text-based version of this tutorial if I forget someone just remind me and

if I forget someone just remind me and

if I forget someone just remind me and there it has all the types there but you

there it has all the types there but you

there it has all the types there but you will use those and then you can actually

will use those and then you can actually

will use those and then you can actually create your own types later on

create your own types later on

create your own types later on basically the types that you’re going to

basically the types that you’re going to

basically the types that you’re going to be using here or gonna be like bool int

be using here or gonna be like bool int

be using here or gonna be like bool int float 32 or float 64 byte I’m probably

float 32 or float 64 byte I’m probably

float 32 or float 64 byte I’m probably forgetting some of the other ones that

forgetting some of the other ones that

forgetting some of the other ones that you’re probably going to use string

you’re probably going to use string

you’re probably going to use string finance a string anyway but you can

finance a string anyway but you can

finance a string anyway but you can check out all the types that you have

check out all the types that you have

check out all the types that you have available we’re gonna use mostly just

available we’re gonna use mostly just

available we’re gonna use mostly just numbers for now just to get an idea

numbers for now just to get an idea

numbers for now just to get an idea we’ll probably throw a string in there

we’ll probably throw a string in there

we’ll probably throw a string in there too but anyway

too but anyway

too but anyway let’s go ahead and create a simple

let’s go ahead and create a simple

let’s go ahead and create a simple function we’re gonna call this funk add

function we’re gonna call this funk add

function we’re gonna call this funk add an add is gonna add together two

an add is gonna add together two

an add is gonna add together two variables like x and y and then it’s

variables like x and y and then it’s

variables like x and y and then it’s gonna return a whatever the addition of

gonna return a whatever the addition of

gonna return a whatever the addition of these two things are so so in Python you

these two things are so so in Python you

these two things are so so in Python you wouldn’t have to add any typing but and

wouldn’t have to add any typing but and

wouldn’t have to add any typing but and go you have to add what are the types of

go you have to add what are the types of

go you have to add what are the types of these these variables so X the type of X

these these variables so X the type of X

these these variables so X the type of X we’re going to say is float 64 and then

we’re going to say is float 64 and then

we’re going to say is float 64 and then for y we’re going to say this

for y we’re going to say this

for y we’re going to say this thank float64 also you need to specify

thank float64 also you need to specify

thank float64 also you need to specify the type if the function is going to

the type if the function is going to

the type if the function is going to return something you have specify of

return something you have specify of

return something you have specify of what type will the return be again we’re

what type will the return be again we’re

what type will the return be again we’re gonna say float 64 now since go is all

gonna say float 64 now since go is all

gonna say float 64 now since go is all about efficiency it’s likely the case

about efficiency it’s likely the case

about efficiency it’s likely the case that you don’t need the precision of 64

that you don’t need the precision of 64

that you don’t need the precision of 64 bits so you could probably use the float

bits so you could probably use the float

bits so you could probably use the float 32 but I’m a man with the plan and I’m

32 but I’m a man with the plan and I’m

32 but I’m a man with the plan and I’m using float64 for a reason so we’ll

using float64 for a reason so we’ll

using float64 for a reason so we’ll continue with float 64 and you’ll see

continue with float 64 and you’ll see

continue with float 64 and you’ll see why soon so return and then we just

why soon so return and then we just

why soon so return and then we just return whatever X + + y is so then we

return whatever X + + y is so then we

return whatever X + + y is so then we can come down here and we could add some

can come down here and we could add some

can come down here and we could add some things together but I also want to show

things together but I also want to show

things together but I also want to show you variables so we’re gonna we’re going

you variables so we’re gonna we’re going

you variables so we’re gonna we’re going to define some variables and then pass

to define some variables and then pass

to define some variables and then pass those variables to the function because

those variables to the function because

those variables to the function because we could just say add and then you know

we could just say add and then you know

we could just say add and then you know 55.5 in something else but we reuse

55.5 in something else but we reuse

55.5 in something else but we reuse variables first so to define a variable

variables first so to define a variable

variables first so to define a variable there’s a few ways we could get away

there’s a few ways we could get away

there’s a few ways we could get away with this or and do this but we’ll just

with this or and do this but we’ll just

with this or and do this but we’ll just show a really basic one first so let’s

show a really basic one first so let’s

show a really basic one first so let’s say we’re gonna call this number 1 so

say we’re gonna call this number 1 so

say we’re gonna call this number 1 so it’s a var num 1 so far the name of it

it’s a var num 1 so far the name of it

it’s a var num 1 so far the name of it and then the type that we’re gonna make

and then the type that we’re gonna make

and then the type that we’re gonna make this so this will be a float 64 because

this so this will be a float 64 because

this so this will be a float 64 because this is expecting the float 64 and then

this is expecting the float 64 and then

this is expecting the float 64 and then we’ll say that equals 5.6 and then we’re

we’ll say that equals 5.6 and then we’re

we’ll say that equals 5.6 and then we’re going to save our num2 again below 64

going to save our num2 again below 64

going to save our num2 again below 64 and that’s going to be equal to nine

and that’s going to be equal to nine

and that’s going to be equal to nine point five now we could do format dot

point five now we could do format dot

point five now we could do format dot print capital P print line and then add

print capital P print line and then add

print capital P print line and then add num1 num2 so we’ll save that and then

num1 num2 so we’ll save that and then

num1 num2 so we’ll save that and then we’ll just pull up the interpreter go

we’ll just pull up the interpreter go

we’ll just pull up the interpreter go run go to go and we get 15 point 1 now

run go to go and we get 15 point 1 now

run go to go and we get 15 point 1 now chances are you’re not going to see

chances are you’re not going to see

chances are you’re not going to see people’s programs that look like this

people’s programs that look like this

people’s programs that look like this they’re going to use a lot more flex or

they’re going to use a lot more flex or

they’re going to use a lot more flex or hand so there’s a lot of things that we

hand so there’s a lot of things that we

hand so there’s a lot of things that we can do to make this quicker in unless

can do to make this quicker in unless

can do to make this quicker in unless text on screen so first of all anytime

text on screen so first of all anytime

text on screen so first of all anytime you’ve got a succession of things just

you’ve got a succession of things just

you’ve got a succession of things just in general in programming if you’ve got

in general in programming if you’ve got

in general in programming if you’ve got like repetition you there’s probably a

like repetition you there’s probably a

like repetition you there’s probably a way to get rid of the repetition

way to get rid of the repetition

way to get rid of the repetition so so anytime you’ve got a bunch of like

so so anytime you’ve got a bunch of like

so so anytime you’ve got a bunch of like a bunch of variables that are the same

a bunch of variables that are the same

a bunch of variables that are the same type or parameters of the same exact

type or parameters of the same exact

type or parameters of the same exact type you can actually just condense it

type you can actually just condense it

type you can actually just condense it so we can get rid of this float64 here

so we can get rid of this float64 here

so we can get rid of this float64 here and just say X comma Y float64 function

and just say X comma Y float64 function

and just say X comma Y float64 function returns a float 64 return X plus y let’s

returns a float 64 return X plus y let’s

returns a float 64 return X plus y let’s just save and run that real quick just

just save and run that real quick just

just save and run that real quick just to show cool

to show cool

to show cool as expected now the next thing is like

as expected now the next thing is like

as expected now the next thing is like the we’ve got multiple variables here

the we’ve got multiple variables here

the we’ve got multiple variables here now the same thing you could do in

now the same thing you could do in

now the same thing you could do in Python you can do here so you could say

Python you can do here so you could say

Python you can do here so you could say var num1 comma 9 to both of type float

var num1 comma 9 to both of type float

var num1 comma 9 to both of type float 64 equals 5.6 comma 9.5 so two things

64 equals 5.6 comma 9.5 so two things

64 equals 5.6 comma 9.5 so two things it’ll get unpacked into those like gnome

it’ll get unpacked into those like gnome

it’ll get unpacked into those like gnome 1 num2 so 5.6 gets unpacked to num1 num2

1 num2 so 5.6 gets unpacked to num1 num2

1 num2 so 5.6 gets unpacked to num1 num2 isn’t 9.5 and so on so we could save

isn’t 9.5 and so on so we could save

isn’t 9.5 and so on so we could save that run that again 15 1 that said

that run that again 15 1 that said

that run that again 15 1 that said people don’t tend to do this in go so

people don’t tend to do this in go so

people don’t tend to do this in go so anything outside of a function you would

anything outside of a function you would

anything outside of a function you would have to define using the var and give

have to define using the var and give

have to define using the var and give the type and all that if it’s inside a

the type and all that if it’s inside a

the type and all that if it’s inside a function you can use some shorthand and

function you can use some shorthand and

function you can use some shorthand and you actually don’t have to give the type

you actually don’t have to give the type

you actually don’t have to give the type go will figure it out when you compile

go will figure it out when you compile

go will figure it out when you compile but the type cannot change after it’s

but the type cannot change after it’s

but the type cannot change after it’s been compiled so it’ll kind of figure it

been compiled so it’ll kind of figure it

been compiled so it’ll kind of figure it out and then once it compiles you could

out and then once it compiles you could

out and then once it compiles you could never change that type again so you

never change that type again so you

never change that type again so you still have to it’s only gonna have one

still have to it’s only gonna have one

still have to it’s only gonna have one type it’s not going to be dynamic every

type it’s not going to be dynamic every

type it’s not going to be dynamic every time the program comes to it it’s just

time the program comes to it it’s just

time the program comes to it it’s just one time so what you could do is you

one time so what you could do is you

one time so what you could do is you could say rather than fari you could

could say rather than fari you could

could say rather than fari you could just say num1 comma mom to get rid of

just say num1 comma mom to get rid of

just say num1 comma mom to get rid of float64 colon equals five point six nine

float64 colon equals five point six nine

float64 colon equals five point six nine point five now we can add those two

point five now we can add those two

point five now we can add those two things together so save come up here run

things together so save come up here run

things together so save come up here run and we get fifteen point one now like I

and we get fifteen point one now like I

and we get fifteen point one now like I said before man with the plan what if we

said before man with the plan what if we

said before man with the plan what if we wanted this to be super efficient

wanted this to be super efficient

wanted this to be super efficient because five point six nine point five

because five point six nine point five

because five point six nine point five we don’t need the precision of 64 bits

we don’t need the precision of 64 bits

we don’t need the precision of 64 bits let’s go to 32 less garbage so we’ll go

let’s go to 32 less garbage so we’ll go

let’s go to 32 less garbage so we’ll go ahead and save that and let’s run that

ahead and save that and let’s run that

ahead and save that and let’s run that but we get there and the air is that we

but we get there and the air is that we

but we get there and the air is that we can’t use num1 which happens to be a

can’t use num1 which happens to be a

can’t use num1 which happens to be a type float64 as a type float32

type float64 as a type float32

type float64 as a type float32 in argument blah blah blah so when when

in argument blah blah blah so when when

in argument blah blah blah so when when this was was given a type go saw okay

this was was given a type go saw okay

this was was given a type go saw okay it’s a float we’ll just give it float 64

it’s a float we’ll just give it float 64

it’s a float we’ll just give it float 64 and then it gets through the function

and then it gets through the function

and then it gets through the function here and it’s like up we can’t use that

here and it’s like up we can’t use that

here and it’s like up we can’t use that right so so yes you can use the

right so so yes you can use the

right so so yes you can use the shorthand but if you wanted things to be

shorthand but if you wanted things to be

shorthand but if you wanted things to be explicit in some way and you want to use

explicit in some way and you want to use

explicit in some way and you want to use flick float 32 for example and not just

flick float 32 for example and not just

flick float 32 for example and not just some default type that it’s gonna get

some default type that it’s gonna get

some default type that it’s gonna get you would need to specify typing

you would need to specify typing

you would need to specify typing information also just in case I don’t

information also just in case I don’t

information also just in case I don’t cover it

cover it

cover it there are also constant so CO and St

there are also constant so CO and St

there are also constant so CO and St obviously to notify that it’s a constant

obviously to notify that it’s a constant

obviously to notify that it’s a constant you have to use the term constant so

you have to use the term constant so

you have to use the term constant so you’ve got to define it like the

you’ve got to define it like the

you’ve got to define it like the tradition of the first variables I

tradition of the first variables I

tradition of the first variables I showed you so yeah like constant I don’t

showed you so yeah like constant I don’t

showed you so yeah like constant I don’t know X in steepness watch okay so okay

know X in steepness watch okay so okay

know X in steepness watch okay so okay those that’s a few things there now with

those that’s a few things there now with

those that’s a few things there now with shorthand like let’s say you had a punk

shorthand like let’s say you had a punk

shorthand like let’s say you had a punk that’s going to return multiple things

that’s going to return multiple things

that’s going to return multiple things so we’re gonna say multiple and then

so we’re gonna say multiple and then

so we’re gonna say multiple and then multiple is gonna take in will say two

multiple is gonna take in will say two

multiple is gonna take in will say two streams so let’s say it takes an a B

streams so let’s say it takes an a B

streams so let’s say it takes an a B string and then it returns which is kind

string and then it returns which is kind

string and then it returns which is kind of it return a B so it returns string

of it return a B so it returns string

of it return a B so it returns string right but since we want this function to

right but since we want this function to

right but since we want this function to be able to do return a comma B ie its

be able to do return a comma B ie its

be able to do return a comma B ie its returning two things so two strings we

returning two things so two strings we

returning two things so two strings we actually unfortunately have to put this

actually unfortunately have to put this

actually unfortunately have to put this again in parentheses watch out there and

again in parentheses watch out there and

again in parentheses watch out there and specify every return type even if they

specify every return type even if they

specify every return type even if they are the same return type you still have

are the same return type you still have

are the same return type you still have to specify both of them so a B string

to specify both of them so a B string

to specify both of them so a B string string so now what we’re gonna do and

string so now what we’re gonna do and

string so now what we’re gonna do and I’m gonna purposely leave them wanting

I’m gonna purposely leave them wanting

I’m gonna purposely leave them wanting them to there for now just to show you

them to there for now just to show you

them to there for now just to show you another interesting thing about go so

another interesting thing about go so

another interesting thing about go so now let’s say we’ll say word one comma

now let’s say we’ll say word one comma

now let’s say we’ll say word one comma word 2 equals hey I already messed up

word 2 equals hey I already messed up

word 2 equals hey I already messed up but oh I messed up twice two double

but oh I messed up twice two double

but oh I messed up twice two double quotes first of all and then also not a

quotes first of all and then also not a

quotes first of all and then also not a saying

saying

saying equals colon equals will say hey comma

equals colon equals will say hey comma

equals colon equals will say hey comma there okay and then we’ll go ahead and

there okay and then we’ll go ahead and

there okay and then we’ll go ahead and format dot prints

format dot prints

format dot prints don’t forget your capital P print line

don’t forget your capital P print line

don’t forget your capital P print line and then whatever the return of multiple

and then whatever the return of multiple

and then whatever the return of multiple is when we pass w1 and w2 so which I

is when we pass w1 and w2 so which I

is when we pass w1 and w2 so which I just say hey there but we’re gonna hit

just say hey there but we’re gonna hit

just say hey there but we’re gonna hit there we’ll go ahead and run it anyway

there we’ll go ahead and run it anyway

there we’ll go ahead and run it anyway and there you go you see num1 is

and there you go you see num1 is

and there you go you see num1 is declared not used and num2 this is

declared not used and num2 this is

declared not used and num2 this is interesting it kind of makes development

interesting it kind of makes development

interesting it kind of makes development sometimes hard as you’re trying to build

sometimes hard as you’re trying to build

sometimes hard as you’re trying to build a program in and output things as you go

a program in and output things as you go

a program in and output things as you go so if you really wanted them wanting

so if you really wanted them wanting

so if you really wanted them wanting them to you like you’re planning to use

them to you like you’re planning to use

them to you like you’re planning to use it but you just wanted to make sure you

it but you just wanted to make sure you

it but you just wanted to make sure you got to a certain point you could just

got to a certain point you could just

got to a certain point you could just comment them out so a comment is just a

comment them out so a comment is just a

comment them out so a comment is just a single line comment it’s just gonna be

single line comment it’s just gonna be

single line comment it’s just gonna be two slashes a double line comment is

two slashes a double line comment is

two slashes a double line comment is gonna be you know forward slash star or

gonna be you know forward slash star or

gonna be you know forward slash star or asterisk asterisk florid slash and that

asterisk asterisk florid slash and that

asterisk asterisk florid slash and that that would work over multiple lines okay

that would work over multiple lines okay

that would work over multiple lines okay so you could just comment them out if

so you could just comment them out if

so you could just comment them out if you wanted to use them but we’re pretty

you wanted to use them but we’re pretty

you wanted to use them but we’re pretty much done with them now so i’m gonna go

much done with them now so i’m gonna go

much done with them now so i’m gonna go ahead and delete them now the function

ahead and delete them now the function

ahead and delete them now the function that you wrote is and it’s not a big

that you wrote is and it’s not a big

that you wrote is and it’s not a big deal but also if you made an import that

deal but also if you made an import that

deal but also if you made an import that you don’t use that’s gonna throw an

you don’t use that’s gonna throw an

you don’t use that’s gonna throw an error so we’ll save that and we should

error so we’ll save that and we should

error so we’ll save that and we should see hey there this time and we do that’s

see hey there this time and we do that’s

see hey there this time and we do that’s good so there’s that so now a couple

good so there’s that so now a couple

good so there’s that so now a couple more things i want to show before i wrap

more things i want to show before i wrap

more things i want to show before i wrap this one up is like you can do like if

this one up is like you can do like if

this one up is like you can do like if you wanted to convert a type like you

you wanted to convert a type like you

you wanted to convert a type like you could save bar a int B equals 62 and

could save bar a int B equals 62 and

could save bar a int B equals 62 and like you want to say like Barbie float

like you want to say like Barbie float

like you want to say like Barbie float 64 equals and then you can just convert

64 equals and then you can just convert

64 equals and then you can just convert it so you could say float64 a and that

it so you could say float64 a and that

it so you could say float64 a and that will convert a to a float at 64

will convert a to a float at 64

will convert a to a float at 64 also you can they type inference works

also you can they type inference works

also you can they type inference works so you could say like we’ve got VAR a

so you could say like we’ve got VAR a

so you could say like we’ve got VAR a int 62 you could then say X Colt loops X

int 62 you could then say X Colt loops X

int 62 you could then say X Colt loops X colon equals

colon equals

colon equals a and X will be type int okay so you can

a and X will be type int okay so you can

a and X will be type int okay so you can kind of just keep that in the back your

kind of just keep that in the back your

kind of just keep that in the back your mind so I think that’s all I want to

mind so I think that’s all I want to

mind so I think that’s all I want to show on typing information and just one

show on typing information and just one

show on typing information and just one more basic thing that you’ve got to get

more basic thing that you’ve got to get

more basic thing that you’ve got to get used to especially if you’re coming from

used to especially if you’re coming from

used to especially if you’re coming from Python you’ve never had to think about

Python you’ve never had to think about

Python you’ve never had to think about that if you have questions comments

that if you have questions comments

that if you 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 will see you in

them below otherwise I will see you in

them below otherwise I will see you in the next tutorial

”

Be First to Comment

Leave a Reply

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