A robust data independent platform

If you read my most recent blog (The 10,000 Hour Button) you know my plan was to write a series of articles about how the team at GeoSpark Analytics is implementing our new AI-driven natural language…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Simple Web Application with Go

Navigate to any directory on your machine using Terminal, PowerShell, or command prompt, once you have Go installed, and create a new directory called simple-server. That process might look like this:

Now, with all that environment setup out of the way, let’s start writing some code. What we’re going to build is really simple, and it looks like this:

We’ll begin by implementing the main function. It’s responsible for creating the server and registering the handlers (i.e. routes) that will receive HTTP requests and return responses to clients. This main function is a good starting point for building your web applications with Go:

So, you’ve probably already noticed the Handle function on the ServeMux type. Let’s look at the logging and index handlers that are added to see what’s happening in more detail. First, here’s the logging middleware:

A majority of the information you’ll need to properly handle requests by HTTP verb, make decisions based on the URL, and access HTTP headers, are all in the documentation of the http.Request type, so look there first whenever there’s something you’re not 100% sure how to do.

Let’s take a look at the index handler now to understanding how content is usually rendered in Go web applications:

As you can see, this function returns a http.Handler too, which we create similarly to the one returned by the logging middleware. This handler is responsible for rendering the site’s index page, which it does by executing a cached template named “base”, and populating the result with values we specified in an instance of an anonymous struct type. We then set the response status header and return. We don’t have to explicitly set the 200 OK status here because Go does that by default, but it demonstrates how to set status headers.

In a more complex handler, it’s important to be able to verify that it’s functioning as expected. This is usually done with one or more unit tests that verify things like the request response is correct, there’s a valid body, content is properly persisted to the database, etc. So, here’s an example of how we can do that in Go for this simple index handler:

At this point we have handlers for responding to client requests and templates for displaying content, so what’s next? Well, if you look at the templates again, you’ll see a few link and script tags for some additional static files containing CSS styling and JavaScript scripts. Go has a built in http.FileServer handler for serving this type of content. Adding it to the server is similar to how we added the index handler and logging middleware — by declaring a function that returns a http.Handler:

and open http://localhost:8080/ in your browser, you’ll see something like this in the terminal:

That’s it! A simple web application written in Go. Hopefully you can see that creating a simple web application using Go isn’t as difficult as it could be if you chose some of the other languages and frameworks that are out there. It’s possible to configure a secure and fast web server, add templates, serve static content, and test your application using just the standard library.

Add a comment

Related posts:

HOW TO GET A CREDIT CARD?

Ok So i have a good credit score but i keep getting denied for any big credit card company and the only way to raise my credit score is to get a major credit card. are the credit card companys stupid…

Residential HVAC Company For Hire in Vancouver

This post listed below pertaining to Home Ac Repair is without a doubt enlightening. Have a go and draw your own conclusions. Pioneer Plumbing has built a reputation of honesty with our long time…

How does it feel?

The great part of the love I received was through food and practical assistance. I do not want only this anymore. I want to be heard and cuddled. “How does it feel to be abused?”, she asked on…