Press "Enter" to skip to content

Syntax – Go Lang Practical Programming Tutorial p.2


you made it part 2 congratulations so so
in this tutorial we’re just gonna be
kind of taking it a little closer look
at the the kind of intricacies that are
going on here and then cover a few more
things that we didn’t talk about in the
in the previous tutorial so again I’m
gonna start with a clean slate just
because we have a lot of code anyway so
it’s not a big deal
but everything is pretty much gonna
start with package main and then we get
to the imports now in the case of the
previous tutorial we just had format but
if you wanted in most cases you’re not
gonna write a program that has a single
import that’s just not likely so you
could you could import other things this
way so math is another package that’s
part of your standard library but this
is not really the method that you should
use so instead what you’re gonna do is
you’re gonna import and then you’re
going to import in parentheses and then
you’ll just kind of add everything in
here that you’re gonna wind up using so
math the other thing I think go has go
does have like a Styles document but I
doesn’t appear to me that the go
community is as pushy about styles
obviously there’s some things like tabs
and stuff like that they want you to do
and like indenting and stuff like that
that that something like sublime is just
gonna do for you but anyways I just
thought about that as I tabbed over a
math to be in line with format
there are rules somewhere I’ll link to
them at some point in the in the text
base tutorial but you could probably
find them as well but if things like how
many spaces like you should probably
have like an empty line between packages
and import and then at least like one
white space between functions stuff like
that I kind of like to have two white
spaces I honestly don’t know what the
rule is and I’m not really sure the
community has has fully decided on that
but anyway sorry if I if I make somebody
angry here anyways that’s how you would
import multiple packages now the next
thing is let’s do let’s go ahead and use
something from math because as you saw
before we
we did well let’s go ahead and just funk
and so so before we have format dot
print L and and then we printed
something right and we’re gonna go ahead
and continue doing that but we can also
we could say something like let’s do the
square root of four is and then comma
math dot SQ RT and then floor will pass
that through now something you might
notice is the usage of casings so it’s
not by mistake so capital peak and
capital S here in go the cat if the
first letter is capitalized that
function will be exported by go and or
that element for that matter so if you
do not capitalize that first letter it’s
considered to be kind of like an
internal thing whereas so like if you’re
using a package you can rest assured
that all the functions that you’re using
from that package will start with the
capital letter if they didn’t I don’t I
just that can’t exist so so so so just
just know that and get used to using
that kind of capital letter there so
that’s another thing to think about now
the next thing is like if you’re coming
from Python this main function like we
never caught you know we never said pay
main run what happens you go is the main
function is considered your your main
loop so to speak it will always run it’s
like the init method in a class or
something like that in Python language
it’s just always gonna run so whatever
you pass through here will run so for
example if you did you know func foo and
then you know I don’t know put some
thought we gonna put this in here so
we’ll just cut paste and we save that if
we actually go to run that I’ll bring
over the command line here go run go tot
go we see nothing actually happens
because the only thing that it’s not
like all your functions run it’s just
the main function will run so now we
could pass
phoo in here and run that again and then
now you see yes the square root of 4 is
2 okay so so there’s that now also
sometimes you’ll have like packages
inside of packages also let me fix this
so so in this case you’ve got you know
format math and then like let’s say for
example we wanted to import so like
square root is sum is is is a function
that’s just right in the math package
but then there’s also kind of sub
packages in math one of those is random
so RND so if so if you like in Python it
would be a dot notation but in the for
the imports it’s going to be slash and
you wouldn’t be able to say like down
here math /r and slash whatever you plan
to use or dot whatever you’re planning
to use and you also couldn’t do math dot
R and like that that’s not gonna work
you have to actually you’ll have to
import the packages and full up here all
the way to the point where you’re going
to use that actual function so so math
/r and and then now what we can actually
do is is let’s go ahead and I’m gonna
get rid of the Foo function we don’t
really need that anymore and then I’m
just gonna say format dot capital P
print line and then what we can say is
something like a number from 1 to 100
and then we’ll just say Rand dot capital
I and T N and then 100 so this will just
generate a random number up to a hundred
and come over here and oh we didn’t save
it we’re gonna say that first try again
there we go number one to 100 is 81 and
in fact that might actually be 0 to 100
I’ll have to confirm that but that’s
most likely actually or maybe even
better put it’s probably 0 to 99 I’m not
100% on that one so I’m even comment
below if you actually know the answer to
that the intend we could look it up as
well and figure out exactly how that’s
working and in fact let’s just show an
example of that while we’re here so if
you want to know more about a function
or package for that matter or anything
you want to know more about you can use
go doc so for example we could say go
doc from the format package we want to
learn more about a print line so print a
line well hit enter there and then this
just kind of tells us a little bit more
about what we’re doing so it tells us ok
here’s the function it takes a interface
and then you’ve got you know just all
your information basically and it tells
you a little bit more about how it
actually works what it returns all of
that so for that matter we could also do
the same thing with int n I’m not
positive this will be good enough for us
but we’ll see we’ll see what it says
go doc what was it math slash brand and
then int and so basically into n into n
returns yeah do you ran a number and 0
and
so I’m gonna assume that 0 to the number
so I don’t think you would ever actually
be a 100 but anyway someone like I said
feel free to correct me if I’m wrong the
other thing to keep in mind with go it’s
more true or it’s a more honest Rand
random than pythons random so if you if
we run this again I don’t even remember
what the first one was oh it was 81 so
please keep running this and it’s always
going to be 81 if if you don’t want it
to be 81 you have to you have to specify
the the random seed and all of that in
Python you don’t necessarily need to do
that but if you don’t do that in Python
you will notice patterns in in the in
the output of Python but if you don’t
pay close attention to the output of
random you might be under the impression
that yes it’s pseudo-random but it’s
still good enough random for doing a lot
of things but actually even in Python
it’s really not I’m sure a lot of other
languages are the same if they don’t
make you do anything with the seed but
go is going to be truly honest with you
and say ok it’s always 81 until you
change the seed so so that’s pretty
useful anyway so so that’s all for now
just a few more things I wanted to cover
before we actually start getting into
the true grunt work of what’s going on
in go 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