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…

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…