So let's begin with a gentle introduction to the go programming language, and this might be a refresher
for somebody that's worked in go a little bit but needs to kind of refresh their memory.
It might be an introduction to this language for people that are familiar with other languages.
Or it might be the very first time you've ever written a program of any sort.
It doesn't matter.
We're going to go through a series of brief lectures that give us just enough information to begin to
start with Web applications.
So you're going to learn the very basics and go and it's not that hard.
So I'm using visual studio code, which I still don't like and may never like, but I'm going to force
myself to use it for this portion of the course so that I at least know enough to give people a hand
if they run into difficulties.
So I start by starting up visual studio code, which I've done, and I have an empty project.
So if I come in to hear the Explorer and click on that, it says you have not yet open the folder.
So it's open a folder.
I'm going to create a new folder.
It doesn't matter where I create it.
Learning go, but remember where you create it in case you need to go find the files, create and then
I'll go into that folder.
Open.
And in here, I'm going to create an empty file of first of all, I'll get rid of this welcome screen
and then I will create a new file by rolling over the Explorer and hitting this little icon that says
new file.
And I'll call this file.
I mean, I'll go and every go file has to end with Dutko.
It has to have that extension.
So I create it.
Now I have an empty file name Mango.
And as is the case with all go files, the very first thing that must appear to go file is a declaration
that says, what package do you belong to?
And the keyword to make that happen is simply package.
And it is a convention, I can call this Treva if I wanted to, but it is a convention that your main
package is called name.
So I'm going to call my mink's.
I like to adhere to good standards and write what is known as idiomatic go.
All right.
So I've declared this main package and I declare my package is being made.
Every main package must consist of at least one function.
And if you don't know what a function is, don't worry, you're going to find out shortly.
And that function must be called main.
All right.
So I can do that.
How do you declare a function and go with the keyword?
OK, and this one has to be called main.
And because it's a function, it has to have parentheses and a function body begins and ends with curly
braces.
So what are these parentheses for?
First of all, functions have to have parentheses because you need sometimes to pass information to
a function which we're going to do momentarily.
But the main function doesn't take any parameters at all.
So technically speaking, this is a good program.
It has a package named Main.
It has a main package, in other words, and it has one function in there called Main, but it doesn't
do anything yet.
Well, I need to do something.
What can I do?
Well, let's let's think about functions for a minute.
Maybe maybe I can create another function down here called func say say something.
And that's going to take an argument because I need to know what to say.
So I'm going to give it an argument.
And the argument is going to be a string, just a word or a phrase or whatever it's going to be.
It's going to be something stored a bunch of characters in a string.
And because it's a string and string starts with the letter S..
I'll give my variable the rather simple name of S.
You think that might be enough, but it's not.
I need to tell this function and the compiler what kind of parameter this is.
What is is it a number?
Is it a date.
Is it a what.
Is it a string.
Well, in this case it is a string and string is one of the primitives built in to go.
So I have a function name say something that takes one argument s which is a string and it's going to
return some value.
This is the great thing about functions, the little bits of code you can use over and over and over.
You can call them from many places in your program or pretty much any place in your program and a lot
of cases.
And you can return some value so I can return a string.
That's what I want to return.
Now, this is going to be a really stupid function.
All it's going to do is return whatever it's passed.
And how do I return something from a function?
Well, not surprisingly, the word to make that happen, the key word is return.
And I only have one thing I can possibly return, which is a string s, so I haven't done anything else.
All right.
So now I've created a program that has two functions.
The main function, which is the entry point for the application and func say something.
Can I run this?
Let's see what happens.
So I save it.
And you notice when I saved it, it applied formatting, which is rather thoughtful.
How do I run this program?
Go to terminal new terminal window and I'm going to simply write Go, run, maeng, go.
So what should happen?
Well, nothing actually should happen.
Let's try it.
Nothing happened.
Why?
I'm going to have this function.
Surely something should take place.
Well, I've never called this function so I can do that in my main function.
This is the first bit of code that will get executed in the program.
No matter how complex this program is.
This main function is the entry point.
So I can call that function.
And I could do that just by saying say something and say something.
If I just do it like that is going to say, oh, there's an error, you can't go any further.
Why not?
Because say something expects to receive a string.
Well it's just puts a string in there and then go you can write a string by putting quotes around it.
Hello.
OK, I wanted to say hello now surely that's going to do something.
So I've saved my code and I come down here and I'll clear the screen by hitting control and I'll run
it.
Well, nothing happened well, nothing happened because I called the function say something which returns
a type of string, and I didn't do anything with that string.
I need to store it somewhere.
I need someplace to hold that value, whatever it returns back.
And I do that with a variable.
So let's create our first variable and we're going to create a variable two different ways.
But this is the first way we're going to do it with the keyword VA.
VA is a variable and the variable has to have a name.
And I'll call this what to say.
Variables can't have spaces in there.
They can't start with the number.
Pretty much anything else is good to go.
But the approach that I'm using here, which is to start with a lowercase letter and then capitalize
all the other ones cavalcades, that's the convention.
In some case, you're going to want to start with a capital letter, but we'll get to that when we get
to that.
Right now, I've got a variable what to say and it still has an error y because as I said a while ago,
go is a strictly typed or strongly typed language.
It has to have a type.
I'll give this the type string, which is the only type we know about right now.
So perfect.
I've created a variable, but I haven't done anything with it.
And Visual Studio Code has helpfully reminded me that this is a problem.
I have an error.
So I see Red underline over here and over here on the right hand side, I see a little red thing in
the bar that says there's an error in your code right there.
Well, what is there?
I can roll the mouse over it and it will tell me what to say declared but not used.
And this is one of the nice features of go.
If you create a variable, you have to use it.
Otherwise the program will refuse to compile and that is a good thing.
You do not want unused variables floating around your code base doing absolutely nothing, just cluttering
it and taking up space.
So I need to use this variable.
Well, I was a minute ago, I said I need somewhere to hold the value returned by this function.
This is the perfect place to use it.
What to say is equal to say something.
Well, how come I still have an error?
Because even though I've created this variable, initialized it when I created using the keyword, it's
just created as an empty string.
There's nothing in there.
It's a type string, but it has no characters in there.
And even though I stored a value in it here, I still haven't done anything with that variable.
I need to do something.
So what am I going to do?
I'm simply going to print it to the terminal.
How am I going to do that?
Well, there's a number of ways of doing it in the little hollow world example we had early on in the
course we use the FMT print line function.
This time we're going to use a different package that does pretty much the same thing for our purposes
today.
Anyway, I'm going to use the log package log, which is a reserved word so I can't have a variable
name variable named Log.
It is a package and if I hit DOT Visual Studio Code says, hey, you're using the package log, it has
all these functions.
Which ones do you want?
Well, I'm going to use print line Priam TLM and log print line is a function.
Therefore it must have parentheses and it expects but does not require.
In this case, it expects something to be in between those parentheses, the values that I'm passing
to that particular function print line and what I want to pass it is what to say.
And if I say this.
But if I save it after I give the variable to correct and save it, life is good.
Now, I should be able to run this program.
So when I come down here, I saved it.
We're good.
When I come down here and clear the screen and say, go, run, man, go look at what's going to happen.
First of all, it's going to say package means so great.
We're the main package.
That's the one that has to run first.
This is my entry point and we're going to use the funk machine, which is the absolute entry point.
But where did this come from, this import log?
Well, when I imported when I use the package log, I need to tell my program that I'm using that package.
I need to import it.
And once I import it, I have access to all of the functions that are built into that package.
And as we saw a minute ago, we can look again logged.
All of these functions are part of that package.
Therefore, I can use them.
So some are some are other things.
But we they're all available to us.
If I don't import it, I can't use it.
So this idea, like every good idea out there, integrated development environment, when you use a
package, it does the imports for you.
If it's not working for you or you're just using a text editor or whatever you need, you need to manually
type in import log.
But we're using visual studio code, so life is good.
So I go to run this.
It'll say, where's the main package?
Here it is.
Where's the main function?
Here it is.
What do you want me to do?
First of all, you're declaring a variable.
Perfect.
I've created a variable.
It has an empty string now in I want you to store into what to say this variable, whatever the function,
say something returns so I can say it calls it.
I passed it.
Hello.
Say something.
What do I do with it?
I just return whatever you pass me.
So I'm storing this value in this variable and now I'm printing it out.
Very good.
I can use that function again and I can do it this way.
I can say declare another string variable var say something else.
And it's also a string of other spills, something right, and then I can just say down here, say something
else is a sign, the value of say something and I'll pass it goodbye.
And then I will print that to the terminal because I can't declare this variable even if I store something
in it, unless I do something with it.
So log line.
Say are something else, some.
What am I doing here?
Loved up front line, say something else.
All right, now I want to run it, even though I'm calling the same function, say something, I'm passing
at a different parameter.
So I should, first of all, call the function, get the word hello, print it out, call the function
again, store the variable somewhere else and print that out.
Let's see if it works.
Hello, goodbye, and I can even do it this way, logged print line without a variable print line,
say something finally, it doesn't matter what I put in there, so I save that.
And now I'm passing a function as a parameter.
I'm actually passing that call to this function as a parameter to log print line.
Will that work?
Yes, it does.
Perfect.
All right.
Last thing I want to do, I want to talk about some different variable types.
So I'm going to create another variable.
I'm going to call this are because I want it to be not of type string, but of another primitive type
it and it just holds whole numbers.
So down here, after finally I will give a lecture.
Let's not give it a value.
Let's see what happens if we leave it on initialized.
If I say log dot.
Print line.
I declared a variable.
Now I'm using it, but at no point did I give it a value.
What's it going to print out there?
That's going to clear the screen.
Run the program.
And apparently the value of the type and if you don't give it a value, defaults to zero.
That makes perfect sense.
But if I give it a value here, I is equal to seven and save it and clear the screen and run the program
again.
Now I have seven.
What if I give a different value?
I equals eight.
Well, it seems useless.
I assigned the value of seven to I and I immediately assign the value of eight to what does it get.
Go Roumain go perfect.
It has eight.
Last thing I want to talk about just for fun, ok is that functions can actually return more than one
value and this is unusual.
You don't see this in a lot of programming languages.
Java doesn't have it.
You can do the same thing in by returning an array but go it has first class functions.
You can return more than one value.
So let's change our function.
It's going to return to strings, string string.
Now, when you have more than one thing being returned, you need to wrap those in parentheses.
That's it.
So I want to return a string and a string.
Now I immediately have an error.
First of all, say something returns to values and you're only storing it in one.
So that's bad.
Here, say something is supposed to return to values, but you're only returning one, so that's bad.
So we need to fix this.
So we need to return another value.
So let's just return hardcoded string world there they are gone.
But up here I have two errors.
How am I going to get the two values string string into a on this side of the parentheses?
Well, as you might expect, the very first thing I'm returning is whatever was passed to me.
The second thing is the key word is the is the word world, which is just a string.
So I need somewhere to store that.
Well, I can do it like this.
VAR world string.
OK, and then here I could say I want you to take the second return value and staut and world.
OK, that works world.
Rule declared safe.
So that's gone.
I'm still not using it and here where I call the function again world.
Save and down here when I don't need to do anything because I'm not doing anything with the return values,
but it says you're still not using the word world.
Well, I don't want that.
That's a totally unnecessary second parameter.
And I put it in there purely for illustrative purposes.
I actually don't even want to use the word world.
It's just a complete waste of time.
What I can do instead of storing the value is just tell the compiler, ignore the second return parameter.
And that's what the underscore character means.
Ignore the second return parameter.
That's what the underscore means.
And now I should be able to, once I save this, come down here, clear the screen and run the program.
And it should work.
And it does.
All right.
So you might notice that go is rather rather strict other programming languages like P, P or not.
And I'll give you an example of that.
So I have running in an idea over here and I've created a new empty file, which I called maned up.
And if you don't know or don't care about it, just ignore this.
But all files have to start with that little token.
With a question mark on the pointy bracket and variables and start with the dollar sign for reasons
I'll never understand, but they do so I can call this s four string is given the value of hello.
That's no problem.
It works.
And then I'm down here, I can say Echo Dollar Sign s to print that to the screen.
So now when I run this run maned not run meaned up, it runs prints out the word hello perfect.
But I can now do this dollar sign as is equal to seven.
I can store an intern there and the program will run just fine.
No problem.
It prints out seven and I can to create and create another variable.
VAR two is given the value of zero and even though I'm not using it, we will run.
And there's a reason for that is an interpreted language, which means any time you run a file on the
same is true of Python, for example, or JavaScript.
It has to be run through an interpreter and an interpreter takes that file, loads it from disk, loads
it into memory, passes through it, runs it through the interpreter, produces machine code and then
executes the instructions, which means by default, interpreting languages are pretty slow, in contrast.
Go is a strongly typed, compiled language, which means we don't actually ever serve or do anything
with mango, all of our go files and by the time we're done, this course will have many go files in
different packages.
Even they're compiled into a single program, a binary.
And that binary is executed and it is much, much faster.
And because it's compiled, go has the ability to say, well, you're not using that that variable.
I'm not going to let you do that.
In contrast, over IP, we can do this.
We can have variables that change type because it's not strongly typed.
We can also have variables that are never used because we need to load it into disk and execute it through
the interpreter before we can actually do anything with it.
So it's possible to have sloppy code go more or less forces you to be concise and only to include things
you're actually using.
So the other advantage, of course, is a compiled language is much, much faster than an interpreted
language.
Go is, on average, more than two hundred times faster than a similar function executed in say so it
has that going for and it forces me to keep my code clean, which makes me rather happy.
All right.
That's enough for this time around.
In the next lecture, we will begin to look at structure and types in defining our own variable types.
And we'll also look at a few more primitives along the way.
So far, we've only seen string and we also have time.
The time type, we have float's.
We have other primitives.
You should be familiar with those and you will be by the time we get through this little introduction
refresher.
See you in the next lecture.