Menu
  • Home
  • About Me
  • Blog
  • Github
  • LinkedIn

Building Containerized Microservices in Golang Chapter 1 – Go Basic (Installation and basic command)

May 11, 2022May 11, 2022

I’m going to create a Go web application, containerize through Docker multistage builds, deploy to K8s using Helm

concepts that I’ll use:

  • Installing GoLang
  • Installing Go VS Code Extension
  • Go basics:
    • Modules & Packages: GOPATH, GOBIN, go.mod, go.sum
    • Local vs Remote Packages
    • Variables & Constants
    • Functions
    • Maps
    • Packages
    • Cross Complication: GOOS, GOARCH
  • Sample Go Web Application using Gorilla/Mux http module
  • Building the docker image using Multistage builds
  • Writing Docker Compose file
  • Writing Kubernetes Manifest files for the application
  • Creating Helm Chart

GoLang installation

What is Golang?

Download Go: go1.18.1.windows-amd64.msi (132MB)

open any terminal and check go is installed

Once go is installed, we need to set a various path so we will understand three these different environment variables.

GOROOT, GOPATH & GOBIN

  • $GOROOT: denotes go installation path
  • $GOPATH directory contains all the go code(workspace) in the host which defaults to $HOME/go on Unix and %USERPROFILE%/go on Windows.
    • Unix: export GOPATH=${HOME}/go-projects
    • Windows: Create new environment variable GOPATH
  • $GOPATH directory should have three directories
    • src: for source files
    • pkg: for compiled files whose suffix is .a
    • bin: for executable files
  • $GOBIN: directory contains all the final executables. Defaults to $GOPATH/bin or to be set

Create three different directories like this:

open the environment variables and click “New” in system variable

use go env to check:

Module

Go code is grouped into packages, and packages are grouped into modules. A package is a directory of .go files that organize the code into reusable units. A Module is a collection of Go packages, with dependencies and versioning built-in.

Programs start running in package named main, with function main.

package main
import "fmt"
func main() {
    fmt.Printf("Hello, world!")
}

Go.mod & go.sum

Go.mod and go.sum files usually resides at the root of your project. These two files are responsible for precise versioning of the dependency without breaking anything.

Go.sum file contains the cryptographic checksums of the content of specific module versions that you use in your program.

Hello Go World!

Click Source Control, Publish to GitHub
Once you open this go.mod file, you can see the name of module

Let’s try to print “Hello World!”

package main

// import external packages fmt (formatting)
import "fmt"

func main() {
	// Print statement
	fmt.Println("hello, World!")
}

To run this code, go to the terminal and use this command:

go run main.go
go run . # another way to run 
Hello World!

To create executable file, use this command:

go build main.go
exe file should be created.

To remove executable file in current directory, use this command:

go clean

To move final executable into the bin directory, use this command instead of using go build:

go install main.go
main.exe is created in bin directory

To create executable file with different file name, add this argument:

go build -o test.exe

To call a code in the external package, import the package and use the package. Otherwise, it removes the package which is called automatic garbage collection where the unused imports or unused variables will be automatically deleted.

package main

// import external packages fmt (formatting)
import (
	"fmt"

	"rsc.io/quote"
)

func main() {
	// Print statement
	fmt.Println(quote.Go())
}

We have imported a package but it is not available with your local go installation, so we need to download that package.

go mod tidy
go.mod is updated
works!

Variables & Constants

Shorthand declarations:

x := 10
x, y, z := 1, 2, 3
w := 33.546
name := "Hello World"
isWorking := true
func main() {
	var x int = 10
	var y, z = 2, 3
	var name string
	name = "DevOps"
	a := 100
	hello := "Hello world"
	var bye = "bye"
	isWorking := false
	// Print statement
	fmt.Println(quote.Go())
	fmt.Println(x, y, z, name)
	fmt.Println(a)
	fmt.Println(hello)
	fmt.Println(bye)
	fmt.Println(isWorking)
}

Conversion characters

Conversion characters tell Golang how to format different data types. Some of the most commonly used specifiers are:

  • v – formats the value in a default format
  • d – formats decimal integers
  • g – formats the floating-point numbers
  • b – formats base 22 numbers
  • o – formats base 88 numbers
  • t – formats true or false values
  • s – formats string values
  • T – type of variable

example:

package main

// import external packages fmt (formatting)
import (
	"fmt"
	"unsafe"
)

func main() {
	myname := "Sean Lim"
	// Print statement
	fmt.Printf("Hello %s!", myname)
	fmt.Printf("Type of name %T and size is %d", myname, unsafe.Sizeof(myname))
}

Maps

Maps are like python dictionary datatype which is a built-in type in Go. It associates a value to a key, and the value can be retrieved using the corresponding key. If Key is absent, value 0 will be reutned

Syntax: map[key_type]value_type

var daysOfMonth = map[string]int{"Jan": 31, "Feb": 28}   
#or
daysOfMonth := map[string]int{"Jan": 31,"Feb": 28}

daysOfMonth["April"] = 30
fmt.Println(daysOfMonth["Jan"])
fmt.Println(daysOfMonth["April"])

Importing Local Packages

In order to create a new package inside this go module, I’m going to create a new folder inside of src folder.

create a new folder named geometry and file named geometry.go

Earlier, we were importing the functions from the external packages, but now I have created a new package.

// src\go-microservices\geometry\geometry.go

# name this package as geometry
package geometry

# create some functions where my main.go file can refer them or import from this
# use capital letter as the first letter
package geometry

import "math"

func Area(length, width float64) float64 {
	return length * width
}

// Diagonal function that can be exported
func Diagonal(length, width float64) float64 {
	return math.Sqrt(length*length) + (width * width)
}
// src\go-microservices\main.go

package main

import (
	"fmt"

        // import its name of the package
	geometry "github.com/sikgyu/go-microservices/geometry"
)

func main() {
	area := geometry.Area(2, 3)
	diagonal := geometry.Diagonal(2, 3)
	fmt.Printf("Area= %f, Diagonal= %f", area, diagonal)
}

Cross Compilation

Go can compile binaries that will work on the other OS. Go supports a variety of platforms, OS, and architectures

env GOOS=linux GOARCH=386 go build main.go

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts
  • ChinguTalkBot v0.1.0: Setting up AWS Cognito with CDK for User Authentication
  • Phoenix & Elixir: Fix PostgreSQL connection refused
  • Demo: Git Log with Shell script to create a release notes
  • Metasploit
  • CyberSecurity Lab – Online Password Attack

Archives
  • March 2024
  • May 2023
  • April 2023
  • February 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
Categories
  • Amazon Interview (3)
  • Ansible (3)
  • AWS (9)
  • Azure (9)
  • Certification (2)
  • ChinguTalkBot Project (1)
  • cybersecurity (3)
  • Data analytics (6)
  • Demo Videos (6)
  • Docker (5)
  • Git (1)
  • GitLab (1)
  • Golang (3)
  • JavaScript (2)
  • Jenkins (4)
  • PowerShell (1)
  • Python (10)
  • Terraform (11)
  • Uncategorized (9)

©2025 | Powered by WordPress and Superb Themes!