Golang in 5 minutes

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

3. Jdoodle.com Online GO Lang IDE

4. The Go Playground

Before deciding which IDE you prefer to work with, you will first need to download the Go software from the official website https://golang.org/dl/

Part B. Get started

The Golang doesn’t support type inheritance, unlike C++, C#, Java, or other object-oriented languages but it does have interfaces and contracts that can be implemented by multiple types. So, understanding polymorphism is also important to being a successful Go programmer.

First, open a new project and select the Go (GOPATH) option. Next, select Go 1.17 (C;\Program Files\Go).

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.

Part E. Creating your Golang web calculator

Let’s start building our Golang web app using html/template library.

You can find the full code in my Github account below.

Lines 1–8: First, import libraries to enable the building of a web app with Golang. Import HTML/template and the net/http library. Next, import the math library and strconv which converts string to Float number.

Lines 10–14: Start the application with the structure of the calculator (the fields that will be imported by the calculator).

Lines 16+: The main function starts with tmpl that is the import of the Html file (forms.html) that will be built after. Next, lines 23–27 shows the fields string value that is imported from the Html (input1,2 and ope). After, lines 29–30 convert the string value from the Html input to a Float64 data type and then calculates the ope field value. The last line is the port (8080).

Finally, we need to create a HTML file based on the Bootstrap 5.1 template.

After, create the inputs and the entire form which include an input for 2 numbers and select (radio input) for choosing the operation (+-*/).

Lines 16- 20: The Golang answer {{.Res}} will show only if the variable is not empty.

Finally, run the Go file, go to the browser by accessing the following IP address, 127.0.0.1:8080, and you are ready to begin your Go journey!

It was helpful? I would love to hear your feedback. e@fhal.org / linkedin.com

https://github.com/emfhal/Golang-Calculator

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The 9 Meta Tags to Use to Increase Traffic to Your Website

A picture of meta tags in HTML code

Deep Dive into Cortex Metrics — Part I

Neon Labs FAQ

eScience is about being bold enough to ask the right questions at the right time

An Update on the Medium App

Switch Between Light and Dark Modes Automatically Windows 10

Build Your Calender App using PHP AJAX

Spring Batch — ItemReader

Emmanuel Fhal

Emmanuel Fhal

Software Engineer

More from Medium

Golang developer — Part 2 — Intermediate of Golang

Importing private repos from Github

Golang Installation on Ubuntu

Delve With VSCode To Debug Go App