So we left off here last time we wrote a very simple hello world web application, everything is built
into one function, the main function, and all it does is it doesn't matter what page you go to, it
just returns.
Hello, world to the screen.
So let's make this a little more useful and we're going to do this really gradually.
First of all, if you have the application running in your terminal window, you need to quit out of
it.
Control seat will stop it and then I'll hide my terminal and let's have a look at what we have here.
So far.
Right now, we have the main function, plus this handler function, which is built in.
It has a built-In anonymous function with the does everything we want it to do.
So this works, but it might be a little more useful.
Let's pretend we're building a website that consists of two pages, so we're going to have the home
page.
So I'm going to create a new function called Func Home, and this is going to be a handler function.
And this is something you need to remember in order for a function to respond to a request from a Web
browser, it has to handle two parameters.
A response writer, which I'm going to call w http response writer, and it has to handle a request
r star, dot star, dot request.
And then of course it has to have the Brace's.
So there is a function that will actually handle requests.
Right now it doesn't do anything, but it does in fact handle requests.
And what I can do is instead of this handler func with an embedded function, I can delete that entirely
and say I'm going to make a http dot handle func.
And it's going to respond to the root level of my Web application, in other words, the homepage,
and then it's going to call a function home.
Well, Hung is actually this function up here and this doesn't do anything.
But I can, of course, do my standard response and just say format F print F F print F W for my response
writer and then just return.
This is the home page and then I'm going to have a second function and this is going to be an about
page.
So I'm just going to duplicate this line just to save copying and pasting.
And I'm going to say for any request that comes to about, I want to go to a function called the boat,
which doesn't exist yet.
But again, I can copy this, paste it here, change it to a boat.
So now this is linked to this.
And I'm going to say instead of this is the home page.
This is the about page.
This is clear.
Now, I have really short lines in my main function that point to external functions, but we can make
a little more complex than that.
Instead of just saying this is the about page, let's have it return a computed function.
So I'm going to have another function func.
And this doesn't actually have it's not going to be a handler.
It's just going to be a standard function that I can do anything with and I'll call it add values.
And this is going to take two parameters, X and Y, both of which are integers.
And it's going to return an integer, which is just not integer.
And I'm going to do is return.
X plus one there.
So now this function isn't called yet, hence my ID, hopefully Graser to let me know that it's unused
but it's going to take to values X and Y, it's going to add them and return that value.
Now, I could have done this in two lines and said some is assigned the value of X plus Y and then return
some instead or and you just get used to this.
There's multiple ways of doing things in certain parts of go.
I could declare a variable sum that's an ENT and then make some.
I'm not creating and assigning the type here.
This is the shorthand.
I could just say some equals X plus Y and that works.
All of those work.
Of course I like to be concise, so the way I'm actually going to do it is to return X plus what.
Great, now you've got to start thinking like a programmer.
Are there any situations where this function might not work?
What if I pass it X and Y, but one of those is not an integer?
Well, actually, it wouldn't compile.
But for the sake of argument, I probably want to say don't just return an ink type, return an integer.
And if there's an error, return that which means I'm returning to types.
So here I can't do the shorthand of X plus one.
I need to add them together.
So I could say if I was throwing an error, I would have to return my values so I would declare a variable.
I'm actually not going to do this because it's impossible to throw an error here easily.
I could say var some sum equals X plus Y and then return that and nil because there's no error.
But we actually don't have returns some and nil and some is in it.
Like I need more caffeine anyway.
That's what I would normally do and I would check for an error here.
I'm just adding to since I know they're going to be ense because it's a strongly typed language, I
can't accept anything that's not an end.
But I can return my value and an error if there isn't any.
Of course, this is a really simple, straightforward function, so I'll just go return X plus Y and
make this return just an int, but we'll create a third page in a minute where it's more possible to
have an error.
OK, so now I've got this function.
I actually want to return it.
So let's go up here and call it, let's say, my sum.
And here the scope of this sum is only within this variable that's going to be equal to add values and
I'll pass it to integers two and two.
And I can't just not use this variable.
I've created it.
So Go says we've got that variable.
I don't want variables hanging around that don't do anything.
So if you're going to create it, you better use it.
So let's change this instead of just returning a string here, let's cut that out there and format as
print F and now we're going to say this is the about page and two plus two is and now I'm going to put
it in there and remember first print F which I better spell right.
S print F actually wants a placeholder and the placeholder for it is percent d if it was a string it
would be percent s but it's a we'll learn more about that as we use it right now.
I need to put it at integer value in there and I just calculated one.
Am I calculated integer is something so I can put that in there and write it out.
And this throws an error which I'm going to ignore right now.
And remember, the shorthand for ignoring and ignoring something that's return by function is to use
the underscore.
So F productive actually returns to things.
Right?
So I'll ignore both of them.
So now I've created a home handler, I've created an about handler and I have these which point to those
functions and this function about actually calls add values.
Now, here's another thing you have to get used to when a function name or a variable name, which will
get to later on, begins with a capital letter, it means it's accessible outside a given package.
It's not true for the main package because you can't call the main package from an external package,
but you may as well get in the habit of doing things properly.
I am never going to call this function from anywhere else, so I'm going to give it a lowercase name
and that hides it.
If you come from object oriented background, if you've done some programming like in Java or even for
that matter, you can think of this as making the function private.
The only places that this function can be called from are within this package.
And the way that go does it.
If it has an upper area uppercase letter for the first letter of the function name, then it's visible
outside of the package.
If it has a lowercase one, it is not.
So I'm going to leave it like that right now.
And we may also get into the habit of giving comments to things as well because they're quite helpful.
A comment is something in a source file that is ignored by the compiler, and it's just there for people
like you and me to read.
And you can start you can write a function with two slashes and the convention in go.
Is that the very first thing that should appear in the comment above?
A function is the function name about is the about the page scandal.
And the same thing with Home Home is the home page handler.
Now, you don't have to give things comments, of course, but you probably should, even though this
one's not going to be visible outside of the package.
I'm going to give it a comment.
Add values, adds to integers and returns the sum and then main, I'll just call.
This is the main.
Application of the entry point, in other words, OK, so there we are.
Now I've got this all built.
Let's compile it and see what happens.
So all I do is open my terminal and go run the main domain.
Go.
That's it.
So it starts.
It seems to be running.
Nothing strange is happening, but I'm going to make it even even simpler.
I'm going to stop this and come down here.
And just before I call my HDB, listen and serve, I'm going to write to the console and I'm going to
do that like this.
I'm going to come to the top just after my import statements and I'm going to say, well, the port
is something I might want to change and I might use it more than one place.
So rather than just putting Port 80, 80 like this, I'm going to create a variable called Port No.
Which doesn't exist yet, and I could declare it inside the main function if I wanted to, but I might
want to use it another function.
So to do that, I'm going to change where I declare this variable and I'm going to say var porked number
equals Colen eighty eight.
And now that I've done that, I can actually use that where I need to.
So I've got my number now and I can use that down here and I already have.
So that's great.
So the simple thing I can do here is say Vanport.
No, I can actually get better than that.
I never want this number to be changed by another part of the application.
So I can say instead of VAR, which is a variable which can you can just overwrite it with a different
string value.
I can call it a constant.
And the nice thing about a constant is it can't be changed by anything else in the application.
So now that I have my number specified, I can come down here and I want to write to the console and
I'm just going to say format, print, line.
And then again in here, I want to use that part number.
So I'm going to use format as a print F.
Starting, not staring, starting application on port, and this is a placeholder, but it's a string
and it's not an int, which is the only thing we've used so far.
So Percent s is the placeholder.
And now I need to say, what do I want to replace that placeholder with?
I want to replace it with Port.
No.
All right.
So let's run the application again.
And now when I start it up, it's going to say starting application on Port 80 80.
So let's go over to my Web browser and refresh this page.
And this is the home page.
So when I get this is the home page up here, that's because I didn't specify anything after the URL.
If I put a slash there, I should get the same thing.
This is the home page.
And down here it says in these handlers, aha, if it has nothing or a slash, go to home.
And the home function just says this is the home page.
Let's try the about page.
So here back in my Web browser about this is the about page and two plus two is for exactly what I wanted.
And what did it do.
It said, well when you made the request, it wasn't just something to ignore that.
And under here we have slash about.
So now return whatever the about function gives us and the about function calls this function add values
adds two plus to the parameters I specified here and stores that in the variable sum, which is an int
because we use the shorthand assignment, this returns and therefore this is an Int.
And down here I just say print to the response rate or w this computed statement.
This is the which I spelled wrong.
So I means we'll fix that.
This is the about page and two plus two is percent D replace percent D with some and two plus two is
four.
So there you go.
Now we can handle two pages but of course we can handle many pages, as many pages as we want to.
But this is sufficient for our purposes for the moment.
So let's stop now.
We'll pick it up in the next lecture.