cancel
Showing results for 
Search instead for 
Did you mean: 

Related Articles

Go in Plain English: Setting things up and Hello World!

Go in Plain English: Playing with Strings

Quick Intro

I'm the kind of guy that I need to get something done to believe I'm learning.

In this short article, I'll show you how easy it is to be up and running with a WebServer in Go.

The Code

Here's the code you need to run a very basic web server:

0151T000003l4INQAY.png

Code Explained

We're importing net/http which is the library that allows Go to handle HTTP requests and do something about them.

Our responder function here is used to create our HTTP response, i.e. what are we going to respond to web browser once we receive a request.

The Fprint function here is particularly useful because it formats whatever you add to second parameter and throws it into first parameter (resp).

The function responder becomes will be our response.

In the main part of the program, we call http.HandleFunc and what it does is it calls function responder if we receive an HTTP request to "/" to handle the HTTP response.

http.ListenAndServe makes sure our HTTP server is listening on port 8080.

Running our Code

I saved my code in draft.go and ran it like this:

0151T000003l4IXQAY.png

This is what I see:

0151T000003l4InQAI.png

Adding more Paths

I just added an IF and an ELSE IF statements:

0151T000003l4J6QAI.png

You need to hit Ctrl+C to stop our previous code and start it again:

0151T000003l4JGQAY.png

But here's what we get when we go to /anotherpath:

0151T000003l4JLQAY.png

Notice that all responses up to now are 200 OK:

0151T000003l4JQQAY.png

If we go to anything else that is not / or /anotherpath we hit our last else statement:

0151T000003l4JMQAY.png

Because we explicitly added http.StatusNotFound then the response is now 404:

0151T000003l4JVQAY.png

That should be enough to get started but this library is very powerful and rich.

The documentation along with all functions are here: https://golang.org/pkg/net/http/

Optimising our Code

If your page has many paths, it's probably better to organise them into functions. In this case, we can use a router such as https://github.com/gorilla/mux but we need to follow the instructions in previous link to clone library locally so it's available to your installed Go version.

After it's all done, we can split the paths into functions and that's way more scalable than using multiple if's and else's:

0151T000003l4JaQAI.png

The major difference here is that we need to create an instance of the router first (my router := mux.NewRouter()) and then we add the appropriate paths per function.

0151T000003l4JfQAI.png

And finally:

0151T000003l4JkQAI.png

Hang on! Why we didn't create a function for any other non-existing paths (404)?

Because that's the default:

0151T000003l4JpQAI.png

Comments
naman1
Nimbostratus
Nimbostratus

hello guys,

 

I know some Python, HTML, JS, CSS, but only from hacking a few things together that other people have started.

What I'm missing is all the back-end stuff. Is there a step by step guide that goes over:

I know how to create an HTML file and CSS file and serve it locally in my browser, but I want to get a live website and start to build components of it: back end scripting, a database, etc. I don't know where to start.

  1. How to get a domain, choose a hosting site, getting set up in I guess AWS?
  2. Then what do I do with my HTML, CSS, and JS files? I'm ok using some sort of bootstrap CSS, but I don't want to use something like WordPress if I can avoid.
  3. Once set up and live, are there other tutorials/books/videos that tell me how to add more components to it and other possibilities such as having users log in securely, etc.?

 

thanks 9apps cartoon hd

Hi Naman,

 

I'd recommend you go through a Full Stack web development course. There are too many things involved. You can use your company's domain and create a subdomain or maybe an existing domain, so many options. For the HTML, CSS and JS files you can spin up a web server in the cloud with Apache or NGINX and copy the files to them. When it comes to logging in securely, you need to use an authentication method. Some companies already have a single-sign-on service ready to go so it's heavily dependent on the circumstances surrounding you. Apologies for late reply but I only noticed there was a commend now here.

 

All the best,

Rodrigo

Version history
Last update:
‎03-Oct-2019 01:44
Updated by:
Contributors