OK, so I'm still in our main Dongo, but I've deleted everything except what you see on the screen
here, I just have a package declaration and empty brain function and I've left say something that function
we created alone.
And I want to talk about eventually I'm going to get to type's instructs, but I want to talk about
variables and functions just a little bit more.
And I want to draw your attention to new ways to initialize variables and alternate ways to initialize
variables that are similar to the one we saw last time around.
So in my main function, I have nothing.
And above that I'm going to declare a variable outside of the main function, which will give me an
opportunity to talk about scope and variable shadowing.
So I'm going to create a variable var s string.
OK, that's one way to declare a variable.
That's what we did before.
There's another way to do it, to declare it and initialize it at the same time.
And I'll call this seven.
So I've declared a variable of type S that the go compiler is smart enough to say I'm going to infer
the type based upon what you're setting it equal to.
This is a string, therefore X is now a string.
And in my main function, the very first thing I'm going to do is say log print line and I will print
that variable X.
So this variable is declared outside of this function.
Therefore this variable is available to every function in this package.
OK, so that is the scope of this variable.
When I declare a variable in here, var that's two equals six.
This is only available inside the main function.
Say something, doesn't know anything about it, but you notice something here that's a bit of a problem.
OK, well maybe don't, let's just give it an example.
So I print out just to make this work.
Let's get rid of have this log print line s.
Let's duplicate that line log print line S.
S two is comma and then two.
And here I'll put S is and then s now you notice here that I've passed two strings to this one that
I've written by hand by putting quotes around it and one that is a variable and I've done the same thing
here.
Logged up print line is what's called a very ADIC function that will be seeing a little bit later on.
It means you can take one or more parameters or even know parameters.
OK, so all this is going to have all this going to happen right now is I'm going to run the main program.
I'm going to say go run bingo.
And it will run this program and say in the main function, assign a sign, the value of six, the characters
six to the string variable to then print whatever the value of S's and print whatever the value of S2
is.
Let's run that.
So let me say this and go run mango.
Seven to six, perfect, that's exactly what I expected to happen, but now let's add a call to this
function down here.
Say something, say something, and I'm going to pass it x, x, x, OK?
Just because I know that's very different than anything I have so far.
So what do you think is going to happen?
I'm going to come down here and say, say something.
And it takes a parameter called s.
I already have an S up here and it's set the value of seven and I have an S down here.
Which s does this refer to.
Does it refer to this s or this.
S It's not clear and that's a clear example of why you need to be careful with your variable names.
Let's run it and see what happens.
So it says, don't forget to print that out log print line tests, and in front of that, I'll put S
from the say something.
Funk, is this safe that cleared the screen?
Run it again.
Let's find out which s that actually is s from the say something funky is X, X, X.
So here you have this variable s, which you might think is this value from up here being overwritten
because we used a the same variable name for the parameter and we can fix that just by saying as three.
And we run out of turns three and now it's run it, so save that and see what happens.
Clear the screen, go run Mingo.
What should happen?
Well, it should say, first of all, print s, which we know is a package level variable with a value
of seven spelled out print s to which I assigned to value to right here so that you print out six spelled
out then I pass x x x to say something which calls that variable s three which should, what should
happen here is it should say s from the say something func is s from up here seven.
So I should see seven, six, seven.
Let's run it.
Seven, six, seven.
Perfect.
That's ideal.
All right, so there's one example of of things you need to pay attention to.
There are other ways that variable shadowing can happen.
I might, for example, come up in the main function, declare a new variable called s using a different
syntax.
So far all we've seen is this vast string.
And I can do that.
Absolutely, I can do that.
But as far as this function is concerned, s every time I refer to it is going to be whatever of value
I give it within this function.
It's never going to pay attention to this one.
So instead, let's let's look at a different way of initializing a variable S.
S and now I'm not going to use the equals sign.
I'm going to use the column equal sine and I'm going to give that the value of eight.
Eventually I'm going to get confused as to which numbers I'm using.
But as long as I keep it to five or less, I should be OK.
So I've created a new variable here in a different way.
And again, this is because the compiler is very smart, because I didn't use an equal sign.
Actually, what it's doing is saying take this variable, whatever, whatever it is, give it the type
of whatever is over here.
That's a string.
So now I have two variables, again, with the same name and it's easy to get confused.
And when you're writing code, you might think when you use the letter s that you're referring to this,
but in fact, you're referring to this.
So don't do that.
Be very careful with your variable names.
OK, but that's one way of getting it.
All right.
Let's move on to a new topic.
Sometimes the primitive variables are not enough to do what you want them to do.
So, for example, let's say that I am thinking about putting a person in a database and there are certain
kinds of information I want to save about that person.
So I want to say first name, last name, phone number, address, whatever it may be, there might
be 30 different things I'm going to store about a particular individual, but I store them in a database.
Now, I could do that if I wanted to by saying via first name string var last name string ver phone
number string var age int there birth date.
And here's a new type that you're not familiar with yet.
But I, I want, I want to store a date, I could store a string, but that's really not a I mean a
string is a string.
It can be anything I want to take advantage of goes strong typing and ensure that when I saw a date
I'm actually storing a date so I could use a built in package.
That's part of the gold standard library called Time.
And the time type I want to store is actually time.
And in that I can store actually a time or a date or a date in a time.
So I'm going to use that.
I could do that and say, OK, there it is.
And every time I wanted to do something with a person, I could pass along one, two, three, four,
five parameters in functions.
What if that gets to twenty function twenty, twenty bits of information I want to pass along.
Suddenly I have functions with twenty parameters that's going to take forever to type it out and it's
going to look ugly on the screen and you'll be scroll scrolling left and right and it gets extremely
confusing.
So we don't do it that way and go.
What we do instead is we define a type and I can call that type user and it is a struct which you may
never have seen before unless you've programmed in C or a similar language.
But a struct is, as its name implies, just a structure.
And I'm defining that by putting curly braces here and saying this struct contains all kinds of bits
of information.
And those are going to be I'm going to call it first name, which is a string last name, which is a
string.
Phone number, which is a string, and I'll tell you why I'm capitalizing that in a moment, but let
me finish typing first age, which is an entry and birth date, which is time.
Time.
And when I say this, it formatted nicely and I can get rid of this, I don't need that.
Instead, instead of having one, two, three, four, five variables, I have one type of variable.
This is not a variable.
This is a type which can be used as a variable.
So let's get rid of this stuff because we don't need it right now.
And let's get rid of this.
Let's get rid of this function altogether, OK?
And save this.
And now I have a main program that does nothing.
I actually don't need that either.
Let me get rid of it.
So I have a main program that does nothing.
I have imported time because I'm using in this struct, but I'm not doing anything yet.
I have this this type defined, but I'm not even using it.
Let me create a user.
So I'm going to do that in my main function by specifying a variable name user and I'm going to use
the shorthand is equal to what is it equal to.
It is equal to a user, which is this one, and it has certain bits of information and the informations
are first name, which I'm going to give the value of Trever and then a comma, last name and a colon
solla and that's enough.
I'm not going to bother populating the rest of it.
The point is, I can specify all of those now.
I've not actually done anything with that variable.
So again, the compiler saying how you declared a variable, but you're not doing anything with it.
That's bad.
And you notice all I did was say this user is of type user with a capital you up here.
And it has these two things filled out, a first name and last name.
I didn't bother filling out the rest of them.
I can, but I'm not going to right now.
How do I access information from that user?
Well, let's use our log function again.
Log print line.
And I'm in that.
I'm going to print user dot first name and that's how I refer to it.
This user has a member or a field called First Name.
I'm going to print that out.
Let's run this, save it, open our terminal and run it and see what happens.
Go run, Mingo.
And it prints up my first name.
What I can also come in here and say print at the last name to use your last name, save it.
Cleared the screen.
Run it.
OK, well what happens if I try to print my birthdate, which I didn't specify.
Let's try that.
I'll put the birth date just for a label, comma, user, the birth date.
Save it and run it.
And as you might expect, because we didn't initialize birth date up here, we didn't give it a value
here.
It actually prints out the year one January the 1st midnight.
All right.
Well, that that actually is pretty straightforward and it's a really useful way to group things together.
So I did also mention to you that I would tell you why I actually put let me say this, why I put these
things with capital letters.
I put them with capital letters because I knew I might need this user type to be available to another
package.
And go is not an object oriented programming language.
If you're familiar with with objects dealing or object oriented languages like Java or Kotlin or to
a certain extent, then you know what you can do in an object oriented language.
You can say here is a function like I do a new function down here, func, whatever, and it doesn't
do anything and I'm not going to fill it out.
I can make this public, private or protect it.
If it's public.
I can refer to that function from outside of the current package in Java, for example, or if it's
a private, you can't even see it outside of that package.
So it's a way to keep bits of your program that you don't want exposed to other parts of your program
to keep that private.
Wigo doesn't have that functionality using object oriented syntax.
Instead, if I declare a front like this, it's only available within this package, my main package.
But if I declare it like this with a capital letter, it's visible outside of that package.
And you can see that by looking at I see that in practice with another package, by looking at the long
package, notice that every single thing here or every single variable here, you know that this one
is a function fatele f it's visible and it starts with a capital letter in the same way this L date,
which is the name of an int, it actually is visible as well, and it starts with a capital letter.
If I have a variable declared in this package, if I put for example, var special string, that's a
private variable only available to this particular package mean.
But if I gave it a capital letter and this isn't true for the main package, but if you have a capital
letter for the name of a variable inside a package, it's visible outside of that package.
All right.
That's why I put things the way that I did.
So I get rid of these functions.
I'm not using it.
I've got a user type that has members and I now have access to them.
If I want to give more information to this, I can simply say the phone number and it's a string so
I could put one, whatever it is, five five five five five five, one, two and two.
There's my phone number now.
I've populated that.
I do have an error here.
You need to end this with a comma.
So that is type that is variable shadowing life seems to be good so far in the next lecture, we're
going to move on and we'll start looking at other data structures, maps and slices in particular.