- read

Golang in 5 minutes

Em Fhal 64

Go is an open-source programming language that enables you to easily build simple, reliable, and efficient software.

The Go programming language (Golang) was created by Google, but it is completely open-source and free for everyone. It’s being constantly improved, with new features, performance improvements, and bug fixes being released around every six months. You can create many types of software with Go, but it is especially popular with developers who create solutions for web app and cloud services. My name is Emmanuel, and this tutorial is for software developers who want to start working with GoLang.

Part A. Installation

You can use Go for many types of software from simple applications that you can run from a command prompt to web-based applications including:

  1. GoLand by JetBrains

This is my preferred IDE but unfortunately, it comes at a cost but you can avail of a free 30-day trial. It is also free for students.

GoLand IDE by Jetbrains

2. Go in Visual Studio Code

Go with Visual Studio Code

Using the Go extension for Visual Studio Code, you get features like IntelliSense, code navigation, symbol search…

code.visualstudio.com

Part C. Explore the basic Go syntax

This section focuses on the basic syntax shown in the screenshots below.

  1. Working with Integer and Float

2. Printing 4 digits after the decimal

3. Print Float, Int and String

4. Converting Int data type to Float

5. Rounding Float to Int data type (importing “math”)

Part D. Creating ‘Hello World’ web app

First, install gorilla/mux by using your terminal and typing “go get -u github.com/gorilla/mux”.

Next, import gorilla/mux and other libraries to start your first application.

package main

import (
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"time"
)

func handler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!\n")
}

// Route declaration
func router() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", handler)
return r
}

// Initiate web server
func main() {
router := router()
srv := &http.Server{
Handler: router,
Addr: "127.0.0.1:9100",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}

log.Fatal(srv.ListenAndServe())
}

Congratulations, you just built your first web app with Golang.