Press "Enter" to skip to content

Simple Web App – Go Lang Practical Programming Tutorial p.5


what’s going on everybody and welcome to
part five of the go tutorial series in
this series what we’re gonna be talking
about is at least the basics of the
built in go web server that you have at
your disposal so this will be the very
start of our our web app that we’re
gonna be building throughout this
tutorial so first of all I just started
with a empty script just because I just
want to start fresh so we’re gonna say
package mean as usual and then we’re
gonna go ahead and import and we’re
gonna have formats and you just
parentheses format and then finally net
slash HTTP this is all these as part of
your standard library so you should have
both of those now we’re going to go
ahead and do is we’re gonna create our
main function so func main and then in
our main function we’re gonna create
begin creating handlers so basically in
web development you’re gonna have
basically you know something has to take
that URL that someone enters in and it’s
gonna have some sort of path to it and
then you have to take that path and then
figure out what kind of function
corresponds to that path so the thing
that’s going to do that is going to be
the the handler so we’re going to use
HTTP handle func
and that’s going to take in the path
that you want to use so in this case
we’re just gonna use a forward slash
which just a forward slash would be the
index right the homepage right there is
nothing there and then we’re gonna have
whatever we’re gonna whatever function
we want to run in this case it’s going
to be index Handler that we want to run
now it’s beyond actually handling
functions we need to have we need our
server right so we’re gonna actually
serve so we’re an HTTP listen and serve
and then we’re the first parameter here
is gonna be on what port and then the
second parameter is like server stuff
that we don’t really need to deal with
right now but you have to you have to
pass something so we’re gonna pass nil
which is the go equivalent based
leave like pythons none okay so so this
creates our server at basically
localhost port 8000 so cool now we just
need we need index handler you have to
make that function so now we’re gonna go
ahead and do is make that function so
let’s say func
index underscore Handler and then index
handler is gonna have basically two
parameters that are gonna come into it
first it’s gonna have W which is gonna
be your writer so in this is truly you
can name this whatever you want it’s
just if you look at other examples this
is exactly how it’s always gonna be in
people’s code so I’m gonna try to stick
with that but you could name that
whatever you wanted W and then obviously
we have to give the type now the type
here is gonna be a HTTP response writer
title so that’s not a built-in go type
that’s a custom type that we’re going to
be talking about we’ll talk about how
about more about those in the next
tutorial I just wanted to you know not
have a ride on my hands if we got to
part five and still what we’re not
actually applying anything so anyway a
response writer so so you need you know
the writer and that’s what’s gonna be
input information onto the page and then
you also need the request so we’re gonna
say are four requests equals Astra’s
HTTP requests
now recall what is asterisk that’s not
the memory address right the memory just
would have been ampersand this is
reading through HTTP request and then
braces and we’re ready to rumble what do
we want to do in our index handler well
we’re gonna do format dot F print now
this is a basically F print is gonna is
gonna format whatever you specified it’s
gonna format based on what you specify
which we’re gonna say is the writer
itself and then and then it’s gonna it’s
got output whatever you asked it to okay
so so it’s gonna using our response
writer it’s going to write
whoa go is meat awesome so um everything
looks good to me let’s go ahead and run
this bad boys so now let’s go over to
our command line go run go types
[Music]
semicolon and found oh right
that’s not necessary don’t put that
comma there okay dragon all right I’ll
approve such a thing to run okay so now
you can head over to a browser head to a
local host on port 8000 so one two seven
zero zero one : 8000 well go is neat
okay
pretty cool one more thing I figure I
might show you before before we leave
and head on to the next tutorial is just
just a little bit more about the handler
so like let’s say you want to add a new
page we’ll copy paste and let’s say you
wanted to do I don’t know about
so rather than index handler now it will
be about and let’s just basically we can
just copy this whole function paste call
this about Handler and now it does the
formatted print of you know expert web
design by sometimes I can’t even spell
my own name
okay and we’ll break this so ctrl C to
break hopefully you know that by now
we’ll run fresh and now we can go to
like the about page so slash expert web
design by Centex
awesome okay so that’s all for like a
really basic example and like yeah I
mean it’s you know for traditional hello
world example which honestly was more
like this go looks pretty good so as I
was
looking into go before I jumped in I
found like a blog post I’m not gonna
call anybody out I forget well who the
name was that wrote it but as a blog
post and it compared basic examples from
go web development to flask with Python
and Ruby on Rails and it kind of showed
basic hello world examples and when you
look at that you’re like oh wow that’s
super simple like goes awesome but those
are so impractical because it’s not
actually like it’s just showing
like the really you know truly basic
information like this like when you
actually what about when you want to
start using like sessions and what about
when you actually want to start like
doing some sort of logical information
right because at this point like yeah
I’ll it goes actually super simple if
you want to have like a static website
with with go but why would you do that
so so I promise it’s not actually this
simple but anyway I think it’s weird
when you look in the go community
because everybody thinks that you can do
everything you because you can do
everything with with the standard
library packages but eventually you’re
gonna want something on top of that I
think bubble see whenever you get
through the end of this series maybe
maybe you won’t so anyways that’s it
were there simple hello world example in
the next tutorial we’re gonna talk about
this because that’s not quite right
that’s a some sort of custom function
there or a custom type rather that maybe
you want to know how to make yourself
eventually you’re gonna need to know so
you’re gonna find out so that’s all
we’re gonna be doing in the next
tutorial if you’ve got questions
comments concerns whatever
feel free to leave them below otherwise
I’ll see you in the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply