Basic Web Programing with Golang

Tram Ho

Go (aka Golang) is a relatively new and hot language in the programming community in Vietnam. With high performance (running faster than Node.JS, PHP, Ruby, … and just a little slower than C / C ++) plus a simple, bright syntax, Golang helps programmers can build systems. High performance backend system easier. Listen, a well-known programming center in Hanoi has changed the backend part of the entire website from Node.JS to Golang. Today, let’s learn the basics of writing servers with Golang.

1. Architecture

Programming web server with pure Golang, we will use net / http package to create server (listen port, process request, create response …). Anyone who has programmed Node.JS finds it quite similar to how we use the http module inside Node.JS.

The net / http library provides us with a stream of data processing as shown below.

The request from the client to the original server will have to go through a component called Multiplexer . The multiplexer analyzes the request (depending on the url or method) and passes it to the appropriate Handler for further processing. Handlers will be functions that call Database , handle request logic according to the request and return data to the Template to display (Template engine as in Node.JS is ejs or jade …). Finally, Multiplexer will return the response to the client.

2. Hello world

  • The first 2 lines for those who have learned Golang will not be strange. That is including the main package and importing 2 libraries, fmt and net / http.
  • The HandleFunc function of the net/http library takes two arguments, the incoming path and a function that contains two parameters. w http.ResponseWriter is where you can edit the response when returning the client, r *http.Request contains the information of the request that the client sends to the server. This code looks quite similar to Express JS.

  • The last line passes two parameters with the ListenAndServe function to open the port, where the client can request to the server. The first parameter is the port you want to open, the second argument is nil , the server will use the default Multiplexer in net/http . If you want to use 3rd party Multiplexer , you can refer to gorilla / mux or http / router .

Run the file

Open a browser and go to http: // localhost: 3000

3. Using a 3rd party Multiplexer

As mentioned in the previous section, we can use 3rd party Multiplexer for Golang server instead of default Multiplexer in net/http library. The default multiplexer may be missing or limited at some point compared to the 3rd party. For example the complexity of obtaining params requests on urls.

Grollia / mux

Instal package grollia/mux :

  • The first line of the main function is to create an instance of gorilla/mux multiplexer, the HandleFunc function to handle the request will also call through the new multiplexer instead of http as using the default multiplexer.
  • The second argument of the ListenAndServe function also passes r instead of nil as above.

http / router

Instal package http/router :

  • Slightly different from the 2 multiplexers above. http/router handles requests according to the functions corresponding to the http methods (very similar to express js). Parameters passed in the callback function are also 3 parameters, not 2 with the form. Taking params from the request is also quite sweet.

More details on functions like POST, PUT, DELETE, … you can refer here

4. Working with the database

We will demonstrate a little demo about Golang manipulating MongoDB database management system.

First, install the mongodb package

Connect with mongodb

Basically connecting to the database with Golang is no different from other languages, we all need to pass MONGO_URI and use the connect function in the mongo library to connect to the database to interact. The above code has a User model of 2 username and password fields representing the data structure that will be saved in the collection .

Run go run database.go and the results will appear if the connection is successful

Manipulate a little with the data

  • After successfully connecting to the mongodb, we will specify the database and the collection will interact directly with the command. If the database and collection not yet exist, mongo will create one for us.

  • The next 2 lines will call to create an instance of the struct User and then call the InsertOne function in the mongo library to add a new record to the collection .

The mongo library provides many functions that interact with the database such as adding, editing, deleting, … Details of the usage and prototype of functions you can see in detail in GoDoc .

References

Share the news now

Source : Viblo