Press "Enter" to skip to content

More web dev basics – Go Lang Practical Programming Tutorial p.9


what’s going on everybody welcome to
part 9 of our go language tutorial
series in this video what we’re going to
be covering is more on the web server
and kind of serving information back to
the browser just some basic stuff that
we should probably cover before we get
into the nitty gritties so with that I’m
starting with a blank screen here I
guess it can serve as there’s kind of a
reminder of the the web development
basics but anyways packaged me we’re
also gonna import a couple of things one
is the just the format and then also net
slash HTTP then we’re gonna have funk
index underscore handler and in the
parameters here it’s W and then that’s a
HTTP response writer and then we had our
for the request which is a point through
to http dot request and now we can begin
the function and let’s just do format
dot F printf using the writer and then
hey there for now and then finally we
can do our funk mein empty parms and
HTTP dot handle funk handle let’s try
our best not to type out swear words
index Handler and then finally HTTP dot
listen and serve you can also if you had
like HTTP for example you could listen
to serve TLS not really gonna cover that
now but you that just letting you know
that exists forty eight thousand and
then obviously you would serve on a
different port but anyway back to what
we were doing past nil go ahead and save
that and let’s make sure that runs as we
would hope so go run go to go touch go
what did this comma that’s gonna keep
getting me some Python you got you know
you’d have to put in that comma probably
to go to Python forget some commas yeah
yeah
since exterior unexpected funk expect
hmmm it’s not jumping out at me with
this issue would be in ducks handler
past W response writer
okay if that should be fine
mean you handle the funk the index
handler hgp listen and serve 48,000 no
what do we use a single quote yes we use
single quotes that’s what’s getting us
if I would have read the error info I
would have seen that immediately all
right let’s try it again
there we go that’s gonna work allow and
then I’ll head to it all right we gotta
hey there so obviously we generally like
that’s just plain text like review the
source of justice is a they’re usually
in HTML we have things called tags so
what can we do to pass tags well we
actually we can pass tags
oops just by writing them in if we
wanted so let’s just do header one for
example and then I’m just gonna restart
the server real quick maybe hit allow
there we go right and now it’s hey there
and then if we refresh the source we see
us that the tags actually came through
and all that so then we could add like a
few more things like let’s just say
maybe two more lines make this maybe a
paragraph tag and then we’ll make this
one a paragraph tag and then we can say
goes fast and simple like so and then we
can refresh our server I’m just breaking
and restarting it so ctrl C and then
just up arrow to aria rerun it basically
and then as you can see we got that on
separate lines and all that and it works
as you would have expected the other
thing I want to show real briefly is
let’s just make another line and we can
do something like this like we could say
use percent s even add percent s
, and then we can put in what we want to
have there so we could say can and and
then we can also say what did I do here
okay sewed up them strong and then pass
variables so you can even add variables
like this using the the person s so it’s
good break let’s rerun it wow it wish I
have to hit a lot every time refresh
pull it over and there you go hey there
goes fast and simple you can even add
variables the next thing I want to show
though is if we were to view the source
here everything is on just like one line
which I mean it’s probably not the end
of the world but in general we would
like this to be organized like you would
have expect especially when things start
getting like if you were gonna actually
code a website doing this like you
probably wouldn’t you’d probably move to
using templates which we’ll show at a
later time but if you were doing it this
way it would be very hard for you to
debug your own website especially if
you’ve got issues like you forgot to
close a tag somewhere it’s really hard
to find it and it’s gonna be equally as
hard to find it if if it looks like that
so so the other thing we could do is you
can actually do like a multi-line print
so this is true with all the prints you
don’t have to use F print you could this
could just be like at your typical print
line for example but what I’m gonna go
ahead and do is like let’s say we wanted
this exact same code so format dot F
print F and let’s just blow just do
these first three I think and then you
can use the little Tilly and then you
can use that to make a multi-line print
so for example we could throw in all you
don’t need the quotes we could throw in
that this and we’ll just throw in this
one just for just to show it
okay we can save that actually I’m gonna
get rid of these two so we’ll save that
and then I’ll restart the server oops we
have a mistake not enough arguments oh
we forgot the W the writer try again
there it goes
refresh and basically kind of the same
thing everything’s on a separate line
and if we were to go view the source
you’d see well it’s almost good enough
but not quite so the issue is the
tabbing is at least yet like in the
start it’s not going to be tabbed weird
but then down here in theory these tabs
are part of the multi-line so it’s
thinking you want those tabs so you
could get rid of them just by removing
the tabs but again that’s probably not
what you would want either and so again
I’ll just kind of let you know that it
does exist that there are going to be
HTML templates and like I said we can
look into those later on
for solving this sort of a problem but
again we can be a source for fresh and
there we go we’ve fixed that problem but
it is kinda neat you could in theory do
quite a bit of programming and make even
some slightly complicated websites just
by passing variables just like we’ve
done here and using all the information
that we’ve learned up to this point we
could actually make a decent website to
be honest but in general I think you’ll
find that using templates will be a
little easier than doing it this way but
anyways I think that’s all I want really
show right now as far as like just kind
of adding one layer on top of the stuff
that we’ve seen so far in the next
tutorial we’re actually gonna start
building our projects so the project
that I wanted to do I just basically
want to do anything but a blog so so the
project I decided that we could do is to
basically make like a news aggregator
using like the sitemaps from a new
source so so that’s what we’re gonna do
and I think that’ll kind of allow us to
bring in a bunch of concepts that we’re
trying to learn and at the same time
kind of actually use them on a real
example rather than something that’s
like a toy example because a lot of
things really do change when you try to
go from like a basic example to actually
applying it so so anyways we’re actually
gonna start building that code that’s
going to start doing that for us we’re
going to do that in the next tutorials
so that’s what you guys have to look
forward to 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