Skip to the content.
Alpine Linux Docker S3CMD Go System Security MySQL InfluxDB Go:Time Dockerize

Why use Go modules

Go modules can be used to:

You get the idea.

How to use Go modules

Please select a project directory that is outside of GOPATH. Say, you have project at the path /var/www/example and you wish to use go modules inside this project. Also, for the purposes of simplicity let’s assume that we just have one single file inside this project called example.go with the following contents:

package main

import (
	"log"
	"net/http"
	"io/ioutil"
	"strings"
	"github.com/tidwall/gjson"
)

func main() {
	rm := "GET"
	url := "https://jsonplaceholder.typicode.com/todos/1"
	payload := ""
	realPayload := strings.NewReader(payload)

	log.Printf("Requesting URL: %s", url)
	log.Printf("Request Method: %s", rm)
	log.Printf("Request Payload: %s", payload)
	req, err := http.NewRequest(rm, url, realPayload)

	req.Header.Add("User-Agent", "HL Cronicle Script/0.0.1")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Printf("Error sending request. Details: %#v", err)
	} else {
		log.Printf("Result Status: %s", res.Status)
		defer res.Body.Close()
		resp, err := ioutil.ReadAll(res.Body)
		if err != nil {
			log.Printf("Error retrieving response. Details: %#v", err)
		} else {
			userID := gjson.Get(string(resp), "userId")
			log.Printf("User ID: %s", userID.String())
		}
	}
}

Just follow the steps below:

  1. Go to the project directory

$ cd /var/www/example

  1. Initialize a new go module

$ go mod init example

  1. Add the dependencies into go.mod file. This also creates a go.sum checksum file.

$ go get -u ./...

  1. Add these dependencies inside a vendor directory

$ go mod vendor

$ go get -u <repo_url>

$ go mod vendor

This is what has been learned from this blog

Get name of struct field using reflection

See: https://stackoverflow.com/questions/24337145/get-name-of-struct-field-using-reflection See: https://play.golang.org/p/Al_m3GYl5j

Using Go as a scripting language in Linux

Making HTTP Requests in Golang

HTTPS for free in Go, with little help of Let’s Encrypt