Generate a self-signed certificate for Golang

Use OpenSSL generate a self-signed certificate for Golang. In this code snippet I created a certificate with validity of 15 years. openssl genrsa -out server.key 2048 openssl req -new -x509 -sha256 -key server.key -out server.crt -days 5475 Once you create the certificate and key you can use it in your Golang HTTP like this log.Fatal(http.ListenAndServeTLS(“:”+port, […]

Continue Reading…

Randomly create a time.Sleep in Golang

To randomly create a time.Sleep in Golang you can use the code snippet below. You may want to simulate a load in your web server and have an arbitrary seconds or minutes before getting the reply. rand.Seed(time.Now().UnixNano()) delta := rand.Intn(60 + 1) // randomly generates numbers 1 to 60 time.Sleep(time.Duration(delta) * time.Second) // sleep and […]

Continue Reading…

Golang SQLite Simple Example

In this post, I will show you a simple example how to use SQLite in Golang. SQLite is one of the popular embedded, file-based database in the market used by companies like Apple, Airbus, Google, Skype, Autodesk and Dropbox. You can checkout the list of well-know SQLite user in this link https://www.sqlite.org/famous.html Requirements Knowledge in […]

Continue Reading…

Developed a Pastebin like website using Golang

In 2017, I created SkyPaste.com to send configuration scripts from Linux terminal to a web browser. The old skypaste is still available at old.skypaste.com. To learn Golang more, I decide to use Golang to rewrite and launch SkyPaste with similar features to pastebin.pl, a text and source code posting website that I saw when I […]

Continue Reading…

Using reCAPTCHA with Golang

Overview Google’s reCAPTCHA is one of the tool we can use to stop malicious internet bots from abusing our web applications. It comes in two versions, reCAPTCHA v2 and v3. Version 3 uses a score based and no-interaction approach to handle bots from humans. Version 2 uses use a checkbox that will require users to […]

Continue Reading…

Develop a whatismyipaddress.com clone website using Golang

Do you want to build a website clone similar to whatismyipaddress.com? It is actually easy to develop. I decided to create this tool because I was working on an automated DNS client that will check my public IP address. Perhaps, somebody might need this in the future. I already made the completed demo available online […]

Continue Reading…

How to parse JSON data without struct in Golang

In using Golang, we sometimes need to parse a JSON data without knowing or specifying a concrete type struct. We can do this by converting (json.Unmarshal) JSON data into an interface{}. We can then use it like a map. Accessing it like for example m[“username”].(string) func AddUserHandler(w http.ResponseWriter, r *http.Request) { log.Println(“Processing Register User”) var […]

Continue Reading…

Golang MongoDB sort number inside a string

// Get List func (z *DocumentTagService) GetDocumentTags() ([]models.DocumentTag, error) { z.DatabaseService.Connect() var o []models.DocumentTag err := (*z.DatabaseService.Db).C(z.Collection).Find(bson.M{}).Sort(“-$natural”, “-documentReference”).All(&o) return o, err } Result [ { “idDocumentTag”: “5b1390f29ebea41854dcc0dc”, “documenReference”: “MCTU/2018/16”, “company”: “MCTU” }, { “idDocumentTag”: “5b1390f09ebea41854dcc0db”, “documenReference”: “MCTU/2018/15”, “company”: “MCTU” }, { “idDocumentTag”: “5b1390d19ebea41854dcc0da”, “documenReference”: “MCTU/2018/14”, “company”: “MCTU” }, { “idDocumentTag”: “5b1390d09ebea41854dcc0d9”, “documenReference”: “MCTU/2018/13”, “company”: “MCTU” […]

Continue Reading…