Press "Enter" to skip to content

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


what’s going on everybody and welcome to

what’s going on everybody and welcome to part five of the go tutorial series in

part five of the go tutorial series in

part five of the go tutorial series in this series what we’re gonna be talking

this series what we’re gonna be talking

this series what we’re gonna be talking about is at least the basics of the

about is at least the basics of the

about is at least the basics of the built in go web server that you have at

built in go web server that you have at

built in go web server that you have at your disposal so this will be the very

your disposal so this will be the very

your disposal so this will be the very start of our our web app that we’re

start of our our web app that we’re

start of our our web app that we’re gonna be building throughout this

gonna be building throughout this

gonna be building throughout this tutorial so first of all I just started

tutorial so first of all I just started

tutorial so first of all I just started with a empty script just because I just

with a empty script just because I just

with a empty script just because I just want to start fresh so we’re gonna say

want to start fresh so we’re gonna say

want to start fresh so we’re gonna say package mean as usual and then we’re

package mean as usual and then we’re

package mean as usual and then we’re gonna go ahead and import and we’re

gonna go ahead and import and we’re

gonna go ahead and import and we’re gonna have formats and you just

gonna have formats and you just

gonna have formats and you just parentheses format and then finally net

parentheses format and then finally net

parentheses format and then finally net slash HTTP this is all these as part of

slash HTTP this is all these as part of

slash HTTP this is all these as part of your standard library so you should have

your standard library so you should have

your standard library so you should have both of those now we’re going to go

both of those now we’re going to go

both of those now we’re going to go ahead and do is we’re gonna create our

ahead and do is we’re gonna create our

ahead and do is we’re gonna create our main function so func main and then in

main function so func main and then in

main function so func main and then in our main function we’re gonna create

our main function we’re gonna create

our main function we’re gonna create begin creating handlers so basically in

begin creating handlers so basically in

begin creating handlers so basically in web development you’re gonna have

web development you’re gonna have

web development you’re gonna have basically you know something has to take

basically you know something has to take

basically you know something has to take that URL that someone enters in and it’s

that URL that someone enters in and it’s

that URL that someone enters in and it’s gonna have some sort of path to it and

gonna have some sort of path to it and

gonna have some sort of path to it and then you have to take that path and then

then you have to take that path and then

then you have to take that path and then figure out what kind of function

figure out what kind of function

figure out what kind of function corresponds to that path so the thing

corresponds to that path so the thing

corresponds to that path so the thing that’s going to do that is going to be

that’s going to do that is going to be

that’s going to do that is going to be the the handler so we’re going to use

the the handler so we’re going to use

the the handler so we’re going to use HTTP handle func

HTTP handle func

HTTP handle func and that’s going to take in the path

and that’s going to take in the path

and that’s going to take in the path that you want to use so in this case

that you want to use so in this case

that you want to use so in this case we’re just gonna use a forward slash

we’re just gonna use a forward slash

we’re just gonna use a forward slash which just a forward slash would be the

which just a forward slash would be the

which just a forward slash would be the index right the homepage right there is

index right the homepage right there is

index right the homepage right there is nothing there and then we’re gonna have

nothing there and then we’re gonna have

nothing there and then we’re gonna have whatever we’re gonna whatever function

whatever we’re gonna whatever function

whatever we’re gonna whatever function we want to run in this case it’s going

we want to run in this case it’s going

we want to run in this case it’s going to be index Handler that we want to run

to be index Handler that we want to run

to be index Handler that we want to run now it’s beyond actually handling

now it’s beyond actually handling

now it’s beyond actually handling functions we need to have we need our

functions we need to have we need our

functions we need to have we need our server right so we’re gonna actually

server right so we’re gonna actually

server right so we’re gonna actually serve so we’re an HTTP listen and serve

serve so we’re an HTTP listen and serve

serve so we’re an HTTP listen and serve and then we’re the first parameter here

and then we’re the first parameter here

and then we’re the first parameter here is gonna be on what port and then the

is gonna be on what port and then the

is gonna be on what port and then the second parameter is like server stuff

second parameter is like server stuff

second parameter is like server stuff that we don’t really need to deal with

that we don’t really need to deal with

that we don’t really need to deal with right now but you have to you have to

right now but you have to you have to

right now but you have to you have to pass something so we’re gonna pass nil

pass something so we’re gonna pass nil

pass something so we’re gonna pass nil which is the go equivalent based

which is the go equivalent based

which is the go equivalent based leave like pythons none okay so so this

leave like pythons none okay so so this

leave like pythons none okay so so this creates our server at basically

creates our server at basically

creates our server at basically localhost port 8000 so cool now we just

localhost port 8000 so cool now we just

localhost port 8000 so cool now we just need we need index handler you have to

need we need index handler you have to

need we need index handler you have to make that function so now we’re gonna go

make that function so now we’re gonna go

make that function so now we’re gonna go ahead and do is make that function so

ahead and do is make that function so

ahead and do is make that function so let’s say func

let’s say func

let’s say func index underscore Handler and then index

index underscore Handler and then index

index underscore Handler and then index handler is gonna have basically two

handler is gonna have basically two

handler is gonna have basically two parameters that are gonna come into it

parameters that are gonna come into it

parameters that are gonna come into it first it’s gonna have W which is gonna

first it’s gonna have W which is gonna

first it’s gonna have W which is gonna be your writer so in this is truly you

be your writer so in this is truly you

be your writer so in this is truly you can name this whatever you want it’s

can name this whatever you want it’s

can name this whatever you want it’s just if you look at other examples this

just if you look at other examples this

just if you look at other examples this is exactly how it’s always gonna be in

is exactly how it’s always gonna be in

is exactly how it’s always gonna be in people’s code so I’m gonna try to stick

people’s code so I’m gonna try to stick

people’s code so I’m gonna try to stick with that but you could name that

with that but you could name that

with that but you could name that whatever you wanted W and then obviously

whatever you wanted W and then obviously

whatever you wanted W and then obviously we have to give the type now the type

we have to give the type now the type

we have to give the type now the type here is gonna be a HTTP response writer

here is gonna be a HTTP response writer

here is gonna be a HTTP response writer title so that’s not a built-in go type

title so that’s not a built-in go type

title so that’s not a built-in go type that’s a custom type that we’re going to

that’s a custom type that we’re going to

that’s a custom type that we’re going to be talking about we’ll talk about how

be talking about we’ll talk about how

be talking about we’ll talk about how about more about those in the next

about more about those in the next

about more about those in the next tutorial I just wanted to you know not

tutorial I just wanted to you know not

tutorial I just wanted to you know not have a ride on my hands if we got to

have a ride on my hands if we got to

have a ride on my hands if we got to part five and still what we’re not

part five and still what we’re not

part five and still what we’re not actually applying anything so anyway a

actually applying anything so anyway a

actually applying anything so anyway a response writer so so you need you know

response writer so so you need you know

response writer so so you need you know the writer and that’s what’s gonna be

the writer and that’s what’s gonna be

the writer and that’s what’s gonna be input information onto the page and then

input information onto the page and then

input information onto the page and then you also need the request so we’re gonna

you also need the request so we’re gonna

you also need the request so we’re gonna say are four requests equals Astra’s

say are four requests equals Astra’s

say are four requests equals Astra’s HTTP requests

HTTP requests

HTTP requests now recall what is asterisk that’s not

now recall what is asterisk that’s not

now recall what is asterisk that’s not the memory address right the memory just

the memory address right the memory just

the memory address right the memory just would have been ampersand this is

would have been ampersand this is

would have been ampersand this is reading through HTTP request and then

reading through HTTP request and then

reading through HTTP request and then braces and we’re ready to rumble what do

braces and we’re ready to rumble what do

braces and we’re ready to rumble what do we want to do in our index handler well

we want to do in our index handler well

we want to do in our index handler well we’re gonna do format dot F print now

we’re gonna do format dot F print now

we’re gonna do format dot F print now this is a basically F print is gonna is

this is a basically F print is gonna is

this is a basically F print is gonna is gonna format whatever you specified it’s

gonna format whatever you specified it’s

gonna format whatever you specified it’s gonna format based on what you specify

gonna format based on what you specify

gonna format based on what you specify which we’re gonna say is the writer

which we’re gonna say is the writer

which we’re gonna say is the writer itself and then and then it’s gonna it’s

itself and then and then it’s gonna it’s

itself and then and then it’s gonna it’s got output whatever you asked it to okay

got output whatever you asked it to okay

got output whatever you asked it to okay so so it’s gonna using our response

so so it’s gonna using our response

so so it’s gonna using our response writer it’s going to write

writer it’s going to write

writer it’s going to write whoa go is meat awesome so um everything

whoa go is meat awesome so um everything

whoa go is meat awesome so um everything looks good to me let’s go ahead and run

looks good to me let’s go ahead and run

looks good to me let’s go ahead and run this bad boys so now let’s go over to

this bad boys so now let’s go over to

this bad boys so now let’s go over to our command line go run go types

our command line go run go types

our command line go run go types [Music]

[Music]

[Music] semicolon and found oh right

semicolon and found oh right

semicolon and found oh right that’s not necessary don’t put that

that’s not necessary don’t put that

that’s not necessary don’t put that comma there okay dragon all right I’ll

comma there okay dragon all right I’ll

comma there okay dragon all right I’ll approve such a thing to run okay so now

approve such a thing to run okay so now

approve such a thing to run okay so now you can head over to a browser head to a

you can head over to a browser head to a

you can head over to a browser head to a local host on port 8000 so one two seven

local host on port 8000 so one two seven

local host on port 8000 so one two seven zero zero one : 8000 well go is neat

zero zero one : 8000 well go is neat

zero zero one : 8000 well go is neat okay

okay

okay pretty cool one more thing I figure I

pretty cool one more thing I figure I

pretty cool one more thing I figure I might show you before before we leave

might show you before before we leave

might show you before before we leave and head on to the next tutorial is just

and head on to the next tutorial is just

and head on to the next tutorial is just just a little bit more about the handler

just a little bit more about the handler

just a little bit more about the handler so like let’s say you want to add a new

so like let’s say you want to add a new

so like let’s say you want to add a new page we’ll copy paste and let’s say you

page we’ll copy paste and let’s say you

page we’ll copy paste and let’s say you wanted to do I don’t know about

wanted to do I don’t know about

wanted to do I don’t know about so rather than index handler now it will

so rather than index handler now it will

so rather than index handler now it will be about and let’s just basically we can

be about and let’s just basically we can

be about and let’s just basically we can just copy this whole function paste call

just copy this whole function paste call

just copy this whole function paste call this about Handler and now it does the

this about Handler and now it does the

this about Handler and now it does the formatted print of you know expert web

formatted print of you know expert web

formatted print of you know expert web design by sometimes I can’t even spell

design by sometimes I can’t even spell

design by sometimes I can’t even spell my own name

my own name

my own name okay and we’ll break this so ctrl C to

okay and we’ll break this so ctrl C to

okay and we’ll break this so ctrl C to break hopefully you know that by now

break hopefully you know that by now

break hopefully you know that by now we’ll run fresh and now we can go to

we’ll run fresh and now we can go to

we’ll run fresh and now we can go to like the about page so slash expert web

like the about page so slash expert web

like the about page so slash expert web design by Centex

design by Centex

design by Centex awesome okay so that’s all for like a

awesome okay so that’s all for like a

awesome okay so that’s all for like a really basic example and like yeah I

really basic example and like yeah I

really basic example and like yeah I mean it’s you know for traditional hello

mean it’s you know for traditional hello

mean it’s you know for traditional hello world example which honestly was more

world example which honestly was more

world example which honestly was more like this go looks pretty good so as I

like this go looks pretty good so as I

like this go looks pretty good so as I was

was

was looking into go before I jumped in I

looking into go before I jumped in I

looking into go before I jumped in I found like a blog post I’m not gonna

found like a blog post I’m not gonna

found like a blog post I’m not gonna call anybody out I forget well who the

call anybody out I forget well who the

call anybody out I forget well who the name was that wrote it but as a blog

name was that wrote it but as a blog

name was that wrote it but as a blog post and it compared basic examples from

post and it compared basic examples from

post and it compared basic examples from go web development to flask with Python

go web development to flask with Python

go web development to flask with Python and Ruby on Rails and it kind of showed

and Ruby on Rails and it kind of showed

and Ruby on Rails and it kind of showed basic hello world examples and when you

basic hello world examples and when you

basic hello world examples and when you look at that you’re like oh wow that’s

look at that you’re like oh wow that’s

look at that you’re like oh wow that’s super simple like goes awesome but those

super simple like goes awesome but those

super simple like goes awesome but those are so impractical because it’s not

are so impractical because it’s not

are so impractical because it’s not actually like it’s just showing

actually like it’s just showing

actually like it’s just showing like the really you know truly basic

like the really you know truly basic

like the really you know truly basic information like this like when you

information like this like when you

information like this like when you actually what about when you want to

actually what about when you want to

actually what about when you want to start using like sessions and what about

start using like sessions and what about

start using like sessions and what about when you actually want to start like

when you actually want to start like

when you actually want to start like doing some sort of logical information

doing some sort of logical information

doing some sort of logical information right because at this point like yeah

right because at this point like yeah

right because at this point like yeah I’ll it goes actually super simple if

I’ll it goes actually super simple if

I’ll it goes actually super simple if you want to have like a static website

you want to have like a static website

you want to have like a static website with with go but why would you do that

with with go but why would you do that

with with go but why would you do that so so I promise it’s not actually this

so so I promise it’s not actually this

so so I promise it’s not actually this simple but anyway I think it’s weird

simple but anyway I think it’s weird

simple but anyway I think it’s weird when you look in the go community

when you look in the go community

when you look in the go community because everybody thinks that you can do

because everybody thinks that you can do

because everybody thinks that you can do everything you because you can do

everything you because you can do

everything you because you can do everything with with the standard

everything with with the standard

everything with with the standard library packages but eventually you’re

library packages but eventually you’re

library packages but eventually you’re gonna want something on top of that I

gonna want something on top of that I

gonna want something on top of that I think bubble see whenever you get

think bubble see whenever you get

think bubble see whenever you get through the end of this series maybe

through the end of this series maybe

through the end of this series maybe maybe you won’t so anyways that’s it

maybe you won’t so anyways that’s it

maybe you won’t so anyways that’s it were there simple hello world example in

were there simple hello world example in

were there simple hello world example in the next tutorial we’re gonna talk about

the next tutorial we’re gonna talk about

the next tutorial we’re gonna talk about this because that’s not quite right

this because that’s not quite right

this because that’s not quite right that’s a some sort of custom function

that’s a some sort of custom function

that’s a some sort of custom function there or a custom type rather that maybe

there or a custom type rather that maybe

there or a custom type rather that maybe you want to know how to make yourself

you want to know how to make yourself

you want to know how to make yourself eventually you’re gonna need to know so

eventually you’re gonna need to know so

eventually you’re gonna need to know so you’re gonna find out so that’s all

you’re gonna find out so that’s all

you’re gonna find out so that’s all we’re gonna be doing in the next

we’re gonna be doing in the next

we’re gonna be doing in the next tutorial if you’ve got questions

tutorial if you’ve got questions

tutorial if you’ve got questions comments concerns whatever

comments concerns whatever

comments concerns whatever feel free to leave them below otherwise

feel free to leave them below otherwise

feel free to leave them below otherwise I’ll see you in the next tutorial

”

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *