So we left off last time with a simple Hello World program, and this time I want to make another hello
world program, but I want to make it a little more complex this time.
Instead of just printing Hello World to the screen, I want to create a Web application that listens
on a particular part so I can hit that application with my favorite web browser and see Hello World
printed to the browser window.
So that is actually pretty simple and it doesn't sound that interesting, but it actually is a lot more
interesting than you think.
And you'll see why shortly.
So I'm back in my Hello World program and I'm just going to modify this one.
And this time, instead of just printing Hello World to the screen, I'm going to comment that oath,
which automatically makes my idy remove that from FMT format, function or package that I don't need
anymore.
And this time I'm going to create a new function within my main function.
And it's you'll see how it works here.
So we're going to use a built in package that's part of the standard library for God called HTP.
And as the name implies, it's one that deals with the web.
So http dot.
And then I get my idy giving me all of my things that I can possibly use from here.
And you can see there's all kinds of things that are related to the Web.
And that's not surprising because God was built from the ground up to be aware of the web and to be
able to deal with it in a meaningful fashion.
So I'm looking for one that starts with the letter H and it's called Handler Thunk.
There it is.
So that's part of the net HDB package, which I can see right here.
So when I hit return to choose that, it should automatically import net HTTP.
If it doesn't, you need to go type that in.
Now this requires some arguments and the arguments it requires are pretty straightforward.
The first thing it wants and we can see when I roll my mouse over it saying it needs a couple of things.
It needs well to start with, I'm going to give it the pat name.
That's the u r l, the uniform resource locator that I want to listen to.
So when people go to my Web application and don't specify anything after the name of the host, here's
what I want to give back to them.
And what I'm going to give them is I'm actually going to inline a function here func and it's going
to have W which is the HTTP response writer.
This is the very first thing that you need to have because this is a handler function for the HTP.
I need to be able to write responses to the user.
So that's what the response writer does.
And I've just called that w you can call it whatever you want to, you can call it yellow if you want
to, but it's a writer.
So I'm going to put W4 response writer and then I need the request and request starts with a letter.
So I'm going to call my variable R this is my parameter for this function and it's a request now.
It's not an EDP request.
It's actually and this may be the very first time you've ever seen this in your entire programming career.
It's a pointer and I can call it a pointer by putting a star in front of it.
Now a pointer is nothing more than an address in memory where some value is stored.
And the reason I have to use a pointer here is because the handler func requires the request to be a
pointer to a request.
So the request is what the Web server or web browser that's hitting my application sends.
It sends a request and then I process that request, whatever it may be, and send back a response.
And every HTTP handler function has to deal with responses and requests.
So I have my my function partly written here.
Let's get back between these two parentheses and actually do the body of the function and the body is
going to be nothing more than I don't know why I have the letter in there.
There should be no erm try that again.
Handle func.
I want to return something and what I want to return is just font which we saw before.
Print line.
Hello world.
OK, so what reimported FMT for me.
The format function and that's all that I have.
So that is pretty straightforward.
But I don't actually want to print a line here because printing a line is just going to print it to
the console.
So let's change that a little bit.
Instead of using print line, let's use F print F and F print F actually requires a response writer
to write to.
Well I have one right here, the W, so let's put it W in there and now everything seems OK.
The only danger is you notice there's a little squiggly line under this.
It might be different in visual studio code but it's probably there in some format.
If I roll over it, it says this is an unhandled error.
Well, one of the cool things about the standard library is I can actually go and look at this actual
built in function within the the the standard library.
And I'm going to do that in.
My idea, by hitting the car, holding down the Comanche and hitting or clicking on it, and that takes
me right to this function f print f it returns.
So here's the description of this function.
The function is called F print F it's part of the package.
If I scroll to the very top, I'm on line two hundred and two.
I don't want to lose my line at the very top.
It's part of the FMT package, so I'm going to go back to line two hundred and two.
So it's a function as part of that package.
It takes as arguments an IO writer and fortunately the response writer for that's part of the package
satisfies this requirement.
So it's a writer and then it has a format string and then optionally some other things after it, which
I'm going to ignore for right now.
So and it returns.
This is the part I'm interested in.
It returns an end, which is an entry and an error which is of type error.
So let's read the description here.
F print f format's according to a format specified and writes to W it returns the number of bytes written
and any right error encountered.
So I'm going to get the number of bytes written.
I don't really want that, but I can look at it so I could say n that's a variable that's going to store
the number of bytes written and the error, which is I'm going to call E R again.
I can call it whatever I want to and then I think it might just go equals, but that's not going to
work.
Now that is actually a function we're going to use a lot this this equals sign.
But right now we're going to use a special one that says automatically format these return types to
whatever this function sends back.
And we saw in the description that F print F returns and int and an error.
So N will be an or an integer and error will be of type error.
I still have red lines underneath these things indicating there's an error.
Actually if I try to compile this, if I say in my terminal, go, run Maeng go, I'll get an error
and the error is actually two errors and declared but not used an error declared but not used.
So I actually am not allowed in go to declare variables unless I actually use them.
I don't really care about the number of bytes written, but let's do something with it.
OK, format f print line and then I'm going to put.
Bytes written all by column and then a plus sign to join what I'm passing it next, and that would be
when you think that would work.
If you're coming from the AP world, that would make perfect sense, but it doesn't because AP is loosely
typed, which means I can store anything I want in a variable, whereas GO is considerably more strict.
It says M is an aunt and you can't print an event like that.
Well, how am I going to print out the number of bytes to the terminal window, which is all I'm trying
to do?
Well, it's not that hard.
Instead of format print line, I'm going to go format.
Print line, I'm still going to use that, and I don't know your format again.
S print F and s print F allows me to take different data types and return them as a string.
So in here I'm going to put a number of bytes written colon and then I'm going to put a placeholder.
D and D is a placeholder that F format s print F is going to replace with some value.
And as you might guess, I want to put any of them there.
OK, so that works.
But I still have an error that I'm not doing anything with.
All right, let's handle that.
Since error is returned by the F print F function, and if there is no error, it doesn't have any value.
I can actually do a simple if statement and an if statement is nothing more than checking one condition
against another and then executing a particular set of commands if the conditions meet whatever criteria
I specified.
So let's try it.
If error is not equal to nil.
In other words, if there is an error, I don't know what it's going to be.
But I know if there is no error, it's going to be nil that I want to do something.
And all I'm going to do here is format, print, line error.
I'm just going to write the error to the console, which is not terribly helpful in a real world program,
but it's suitable for our requirements right now.
So I've now solved the problem.
I've got those two return values from F print F and I handle one.
If there's an error, I print it.
If there's not, nothing happens and then I just print the number of bytes written.
So let's try that.
Let's run this again.
So back down to my terminal.
I hit the up arrow to get my last command and I run this command and then it goes and says, OK, well,
nothing happened.
It started and it ended y well it's pretty straightforward.
I have this HTP handler function which is listening for a request sent by a web browser.
And yet at no point have I started any process that says listen for requests from Web browsers.
So I actually have to do something else beyond this handler function, which in fact is never actually
called.
It's just sitting there embedded in my main function and it never executes.
So now here's how easy it is to start a Web server that listens for requests in go http dot listen and
serve.
And I'm going to listen to a particular part of my computer.
And because I don't want to use a privileged port and ports are just things that you can listen to from
applications.
The first one thousand and twenty four ports on any computer system are privileged.
You have to be a super user to use those, which is no problem.
I'm going to listen to Port and it has to start with the colon.
Eighty, eighty.
So I'm going to listen on port eighty eighty and then it expects another argument.
A handler.
But I'm going to say nil.
I'm not going to pass it.
A particular handler instead.
That's what this HDB handle function does.
I don't need this anymore so I'm going to delete it.
And now when I run this notice, this also has an error.
It's listening for and I'm going to ignore it explicitly.
I'm going to do that because I don't want to handle this error.
If the Web server doesn't start, I will know because the program will crash.
So I don't need to look for this error.
I'm going to say.
Underline so the shift dash equals that and all this is saying is if there's an error, I don't care,
just throw it in the trash bin, pay no attention to it whatsoever.
So let's run this program again and see what happens.
Go, run, go.
And nothing seems to be happening, but it didn't kick me out of the application.
What I have just done with one line of code is started a production quality web server to listen for
requests.
So let's make sure this actually works.
What I expect to happen is I'm going to go to my Web browser.
I'm going to go to this server, which is called localhost on any computer.
That's the default localhost Colen eighty eighty.
And what I want to see happen is two things I want to see.
Hello, world printed to my Web browser window, and then I want to be able to come back in here and
I should see a log entry right down here in the terminal that tells me how many bytes were written.
Let's see if that works.
So I go to my favorite Web browser and I go to the URL bar and I type in local host Colen eight zero
eight zero and it return.
And there is hello world.
Perfect.
Now let's go back and look at our terminal window.
And there it is, number of bytes written, 13 number of bytes written 13.
Now, why did that run it twice?
I'm not sure.
Don't really care about that right now.
I'm going to let that do its thing.
So now I have written a complete Web application.
True.
All it does actually serve the words hello, karma world exclamation mark.
But it does it really, really well.
And this is the basis for any web application and go now things will get more complex, but not that
complex.
One of the great things about Go is it's an extremely easy to learn language and everything functions
in a really straightforward, organized and logical way, and you're not going to have any difficulty
following along.
So that's enough for this time.
Next time will make it a little bit more complex.