Press "Enter" to skip to content

Types – Go Lang Practical Programming Tutorial p.3


what is going on everybody and welcome
to part three of the go tutorial basics
in this tutorial what were to be talking
about is typing information like I said
in the first tutorial we really are
gonna get to actually applying this to
something more useful the thing we’re
gonna apply it to though is go has a
built-in web server does web obviously
like web applications pretty simply
right out of the box even with just the
standard library so that’s what we’re
gonna do but even just to do the most
basic hello world example we still have
to get through a lot of like really
basic truly just basic things about go
especially because I think a lot of
people are coming from Python since
that’s mainly what I do on my channel
and things like today what we’re gonna
be talking about is typing information
which is something you just don’t even
have to think about in Python there’s
some little intricacies here that will
trip you up so anyways let’s go ahead
and get into it so I’m going to for this
one we just really need format and we
can empty out main and let’s talk about
types so in go there’s quite a few types
I’ll put a link in the description to
the text-based version of this tutorial
if I forget someone just remind me and
there it has all the types there but you
will use those and then you can actually
create your own types later on
basically the types that you’re going to
be using here or gonna be like bool int
float 32 or float 64 byte I’m probably
forgetting some of the other ones that
you’re probably going to use string
finance a string anyway but you can
check out all the types that you have
available we’re gonna use mostly just
numbers for now just to get an idea
we’ll probably throw a string in there
too but anyway
let’s go ahead and create a simple
function we’re gonna call this funk add
an add is gonna add together two
variables like x and y and then it’s
gonna return a whatever the addition of
these two things are so so in Python you
wouldn’t have to add any typing but and
go you have to add what are the types of
these these variables so X the type of X
we’re going to say is float 64 and then
for y we’re going to say this
thank float64 also you need to specify
the type if the function is going to
return something you have specify of
what type will the return be again we’re
gonna say float 64 now since go is all
about efficiency it’s likely the case
that you don’t need the precision of 64
bits so you could probably use the float
32 but I’m a man with the plan and I’m
using float64 for a reason so we’ll
continue with float 64 and you’ll see
why soon so return and then we just
return whatever X + + y is so then we
can come down here and we could add some
things together but I also want to show
you variables so we’re gonna we’re going
to define some variables and then pass
those variables to the function because
we could just say add and then you know
55.5 in something else but we reuse
variables first so to define a variable
there’s a few ways we could get away
with this or and do this but we’ll just
show a really basic one first so let’s
say we’re gonna call this number 1 so
it’s a var num 1 so far the name of it
and then the type that we’re gonna make
this so this will be a float 64 because
this is expecting the float 64 and then
we’ll say that equals 5.6 and then we’re
going to save our num2 again below 64
and that’s going to be equal to nine
point five now we could do format dot
print capital P print line and then add
num1 num2 so we’ll save that and then
we’ll just pull up the interpreter go
run go to go and we get 15 point 1 now
chances are you’re not going to see
people’s programs that look like this
they’re going to use a lot more flex or
hand so there’s a lot of things that we
can do to make this quicker in unless
text on screen so first of all anytime
you’ve got a succession of things just
in general in programming if you’ve got
like repetition you there’s probably a
way to get rid of the repetition
so so anytime you’ve got a bunch of like
a bunch of variables that are the same
type or parameters of the same exact
type you can actually just condense it
so we can get rid of this float64 here
and just say X comma Y float64 function
returns a float 64 return X plus y let’s
just save and run that real quick just
to show cool
as expected now the next thing is like
the we’ve got multiple variables here
now the same thing you could do in
Python you can do here so you could say
var num1 comma 9 to both of type float
64 equals 5.6 comma 9.5 so two things
it’ll get unpacked into those like gnome
1 num2 so 5.6 gets unpacked to num1 num2
isn’t 9.5 and so on so we could save
that run that again 15 1 that said
people don’t tend to do this in go so
anything outside of a function you would
have to define using the var and give
the type and all that if it’s inside a
function you can use some shorthand and
you actually don’t have to give the type
go will figure it out when you compile
but the type cannot change after it’s
been compiled so it’ll kind of figure it
out and then once it compiles you could
never change that type again so you
still have to it’s only gonna have one
type it’s not going to be dynamic every
time the program comes to it it’s just
one time so what you could do is you
could say rather than fari you could
just say num1 comma mom to get rid of
float64 colon equals five point six nine
point five now we can add those two
things together so save come up here run
and we get fifteen point one now like I
said before man with the plan what if we
wanted this to be super efficient
because five point six nine point five
we don’t need the precision of 64 bits
let’s go to 32 less garbage so we’ll go
ahead and save that and let’s run that
but we get there and the air is that we
can’t use num1 which happens to be a
type float64 as a type float32
in argument blah blah blah so when when
this was was given a type go saw okay
it’s a float we’ll just give it float 64
and then it gets through the function
here and it’s like up we can’t use that
right so so yes you can use the
shorthand but if you wanted things to be
explicit in some way and you want to use
flick float 32 for example and not just
some default type that it’s gonna get
you would need to specify typing
information also just in case I don’t
cover it
there are also constant so CO and St
obviously to notify that it’s a constant
you have to use the term constant so
you’ve got to define it like the
tradition of the first variables I
showed you so yeah like constant I don’t
know X in steepness watch okay so okay
those that’s a few things there now with
shorthand like let’s say you had a punk
that’s going to return multiple things
so we’re gonna say multiple and then
multiple is gonna take in will say two
streams so let’s say it takes an a B
string and then it returns which is kind
of it return a B so it returns string
right but since we want this function to
be able to do return a comma B ie its
returning two things so two strings we
actually unfortunately have to put this
again in parentheses watch out there and
specify every return type even if they
are the same return type you still have
to specify both of them so a B string
string so now what we’re gonna do and
I’m gonna purposely leave them wanting
them to there for now just to show you
another interesting thing about go so
now let’s say we’ll say word one comma
word 2 equals hey I already messed up
but oh I messed up twice two double
quotes first of all and then also not a
saying
equals colon equals will say hey comma
there okay and then we’ll go ahead and
format dot prints
don’t forget your capital P print line
and then whatever the return of multiple
is when we pass w1 and w2 so which I
just say hey there but we’re gonna hit
there we’ll go ahead and run it anyway
and there you go you see num1 is
declared not used and num2 this is
interesting it kind of makes development
sometimes hard as you’re trying to build
a program in and output things as you go
so if you really wanted them wanting
them to you like you’re planning to use
it but you just wanted to make sure you
got to a certain point you could just
comment them out so a comment is just a
single line comment it’s just gonna be
two slashes a double line comment is
gonna be you know forward slash star or
asterisk asterisk florid slash and that
that would work over multiple lines okay
so you could just comment them out if
you wanted to use them but we’re pretty
much done with them now so i’m gonna go
ahead and delete them now the function
that you wrote is and it’s not a big
deal but also if you made an import that
you don’t use that’s gonna throw an
error so we’ll save that and we should
see hey there this time and we do that’s
good so there’s that so now a couple
more things i want to show before i wrap
this one up is like you can do like if
you wanted to convert a type like you
could save bar a int B equals 62 and
like you want to say like Barbie float
64 equals and then you can just convert
it so you could say float64 a and that
will convert a to a float at 64
also you can they type inference works
so you could say like we’ve got VAR a
int 62 you could then say X Colt loops X
colon equals
a and X will be type int okay so you can
kind of just keep that in the back your
mind so I think that’s all I want to
show on typing information and just one
more basic thing that you’ve got to get
used to especially if you’re coming from
Python you’ve never had to think about
that if you have questions comments
concerns whatever feel free to leave
them below otherwise I will see you in
the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply