Integer and float conversion to string and vice versa in Golang

To convert string to Integer value in Golang you need to import strconv library, then call Atoi function as:

 intval, err := strconv.Atoi("123")

to convert string to float call ParseFloat method:

    floatval, err := strconv.ParseFloat("12.1", 0)

To convert Integer to string use Itoa method:

    str := strconv.Itoa(45)

To convert float to string use FormatFloat method:

    str := strconv.FormatFloat(45.124, 'f', 2, 32)

‘f’ is format type, 2 is decimal points, and 32 is bit size

Sprintf method of fmt library could be used instead to return formatted float string:

    str := fmt.Sprintf("%.2f", 45.124)

f is format type (float) and .2 represents two decimal points

Command line flags in Golang

Parameters could be passed to console applications in command line, example:

./MyTool -f /home/user -count 20

In this case parameter flags are: -f, and -count, and parameter values are: /home/user and 20

To apply this in Golang we add “flag” to import and write below code:

func main() {
     folder := flag.String("f""/", 
        "Folder name that contents the files")
     count := flag.Int("count"10"Files number to process")
     fmt.Printf("Selected folder: %s, files count: %d\n", 
         *folder, *count) 

Example of usage and output:

./MyTool -f /home -count 20
Selected folder: /home, files count: 20

Calling it with –help:

./MyTool --help
Usage of ./MyTool:
-count int
Files number to process (default 10)
-f string
Folder name that contents the files (default "/")

Date and Time format in Golang

Go language is using special formatting tags for date and time, instead of using yyyy-mm-dd as in most languages, it uses a special date to represent format for date and time, this special date and time is:

2006-01-02 15:04:05.0000

which is January 2, 2006, at 15:04:05.0000

Example of printing formatted date and time:

func main() {
today := time.Now()
fmt.Println(today.Format("2006-01-02 15:04:05"))


2020-12-18 15:44:43

For Input date and time as string and parsing it to time, we can use time.Parse(layout, adatetimeStr), example:

func main() {
dateInput := "16/11/1975"
adate, err := time.Parse("02/01/2006", dateInput)
if err != nil {
} else {


1975-11-16 00:00:00 +0000 UTC


Service Check is a 64-bit Linux command line tool to check background services/applications and run it if it is not working

it stores configuration for applications to be checked in checklist.ini file in the same directory of servicecheck tool.

example of configuration for application called goagent located in /home/user/go/

checklist.ini contents:

goagent /home/user/go/goagent

WebSocket using Golang

Inspired by the previous Article of Implementing WebSocket with NodeJS, I have tried successfully WebSocket in Go language.


First install WebSocket package for Go:

go get

Start new project in Go, and write this HTML content in websockets.html file in project directory:

<!-- websockets.html -->
<h2>WebSocket using Golang</h2>
<h3>Server time</h3>
<pre id="output"></pre>

    var input = document.getElementById("input");
    var output = document.getElementById("output");
output.innerHTML =;
    var socket = new WebSocket("ws://" + + "/time");

    socket.onopen = function () {
        output.innerHTML = "Status: Connected";

    socket.onmessage = function (e) {
        output.innerHTML = "From Server: <b>" + + "</b>";



Then write Go source code as main.go in the same directory of .HTML file :

// GorillaWebSocket project main.go
package main

import (


var upgrader = websocket.Upgrader{
    ReadBufferSize: 1024,
    WriteBufferSize: 1024,

var id = 0

func main() {
    http.HandleFunc("/time", timer)

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "websockets.html")

    http.ListenAndServe(":1234", nil)

func timer(w http.ResponseWriter, r *http.Request) {

    conn, err := upgrader.Upgrade(w, r, nil)
    localID := id
    fmt.Println("Connected to: ", r.RemoteAddr, " as client # ", localID)
    fmt.Println(localID, r.UserAgent())
    if err != nil {
        fmt.Println("Error: ", err.Error())

    for {

        now := time.Now()

        msg := "(" + strconv.Itoa(localID) + ") " + now.String()[:19]
        // Write time to browser
        err = conn.WriteMessage(websocket.TextMessage, []byte(msg))
        if err != nil {
            fmt.Println("Closed client # ", localID, ": Error: ", err.Error())

Then run it and in the browser open


You can open many tabs, or open many browsers or clients, and note that timer function will be instantiated with every client and it keeps working, which means every client will have it’s own running timer function, until the browser disconnects, then it will exist and will be removed from memory



ServiceCheck is a native Linux 64 bit utility to check background service and run it if it is not up.


  1. Configure checklist.ini file in the same directory of servicecheck application. Put all services to be checked in that file in the format

app1name      "/home/user/app1/app1name"
appn2name "sh /home/user/app2/"

Application name (that appear in Linux processes (ps -ef)), then script to run the application if it is closed

2. Put servicecheck in crontab job to work periodiacly, for example every 5 minutes, e.g.

*/5 * * * * /home/user/checker/servicecheck

Note: to run applications in sudo privilege configure servicecheck in sudo crontab, for normal user, use crontal for normal user

CouchDB installation in Ubuntu, running admin portal, and Golang connection

Apache CouchDB is an open source NoSQL document based database engine that is written in Erlang. It uses RESTFull web service to be accessed for clients, so that you can use it though REST api or through drivers in each programming languages that has encapsulated REST calls into programming language methods/procedures calls.

CouchDB supports multi node clustering.


To install latest version of couchdb it in Ubuntu 16.04 (xenial):

echo "deb xenial main" | sudo tee -a /etc/apt/sources.list

For Ubuntu 18.04 (bionic):

echo "deb bionic main" | sudo tee -a /etc/apt/sources.list

For Ubuntu 20.04 (focal)

echo "deb focal main" | sudo tee -a /etc/apt/sources.list

Then add that new repository key:

curl -L | sudo apt-key add -

Note that if curl command is not found, you need to install it:

sudo apt-get install curl

Then update and install:

sudo apt-get update && sudo apt-get install couchdb

After installation, type this URL in browser to open administrative portal for CouchDB:

Enter admin login and password that you have entered during installation, then you will get this view:

Then you can create databases, manage data, and users


Accessing server information :



"couchdb": "Welcome", "version": "2.3.0", "git_sha": "07ea0c7",
"uuid": "8e99e2abff3f90efd7643ff8e04f7f8f",
"features": [ "pluggable-storage-engines", "scheduler" ],
"vendor": { "name": "The Apache Software Foundation" }

Creating a database named test :

curl -X PUT
{"ok": true}

Retrieve information about the test database :


{ "db_name": "test", "purge_seq": "0-g1AAAAFTeJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMeC5BkeACk_gNBViIDQbUHIGrvE6N2AUTtfvxqkxyAZFI8QfuTFEDq7Il0ZwPE7vkEzEwAmVlP0MxEhiR5iKIsAD6cXo4", "update_seq": "1-g1AAAAFTeJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMiQ5L8____sxIZ8ChKUgCSSfZgdYz41DmA1MUTNi8BpK6eoLo8FiDJ0ACkgErnE6N2AUTtfmLUHoCovU-M2gcQtSD3ZgEAX4Rejw", "sizes": { "file": 38120, "external": 40, "active": 318 }, "other": { "data_size": 40 }, "doc_del_count": 0, "doc_count": 1, "disk_size": 38120, "disk_format_version": 7, "data_size": 318, "compact_running": false, "cluster": { "q": 8, "n": 1, "w": 1, "r": 1 }, "instance_start_time": "0" }

Create a document, asking CouchDB to supply a document id :

curl -X POST -H "Content-Type: application/json" --data \ '{ "text" : "Wikipedia on CouchDB", "rating": 5 }' \

"ok": true,
"id": "123BAC",
"rev": "946B7D1C"


Go lang connection

First you need to import a library, for example:

And this is an example of creating and connecting to a database called (academy) using Go language:

It contains create database, insert document, and search for documents:

// CouchDBSample project main.go
package main
import (
type TestDocument struct {
	Name          string
	Address       string
	InsertionTime time.Time
type DataList struct {
	Docs []TestDocument
func main() {
	var timeout = time.Duration(500 * time.Millisecond)
	conn, err := couchdb.NewConnection("", 5984, timeout)
	if err != nil {
		println("Error: ", err.Error())
	auth := couchdb.BasicAuth{Username: "motaz", Password: "help"}
	conn.CreateDB("academy", &auth)
	db := conn.SelectDB("academy", &auth)
	today := time.Now()
	theDoc := TestDocument{
		Name:          "Motaz",
		Address:       "Alsajana",
		InsertionTime: today,
	insert(db, theDoc)
	selector := `{"Address":"Bahri"}`
	search(db, selector)

func insert(db *couchdb.Database, theDoc TestDocument) {

	now := time.Now()
	rev, err := db.Save(theDoc, now.String(), "")
	if err != nil {
		println("Error in .Save: ", err.Error())
	} else {
		println("Saved: ", rev)

func search(db *couchdb.Database, selector string) {
	result := DataList{}
	var selectorObj interface{}
	err := json.Unmarshal([]byte(selector), &selectorObj)
	params := couchdb.FindQueryParams{Selector: &selectorObj}
	err = db.Find(&result, &params)
	if err != nil {
		println("Error in .Find: ", err.Error())
	fmt.Println("Num: ", len(result.Docs))
	for _, x := range result.Docs {
		fmt.Println("%v", x)

Comparison to MangoDB

Advantages over MongoDB:

  1. Its license is Apache free license, while MongoDB needs commercial license for close source application, otherwise you have to open source your projects that uses MongoDB, so that MongoDB has been dropped from famous Linux distributions such as: Debian and RedHat.
  2. Uses REST API that can be called from any programming language, or scripts, such as shell script, no driver is needed when using this method
  3. Available in Android and IOS
  4. Supports master to master clustering in addition to master to slave clustering

Disadvantages of CouchDB compared to MongoDB:

  1. Less popular in usage compared to MongoDB
  2. MongoDB is more faster in read
  3. MongoDB is better in rapid growth databases
  4. MongoDB supports more programming languages