Find period between two times with golang

Golang time package provides rich time handling functionality.

Duration type represents passed time between two concrete dates.

start := time.Now()
end := time.Now().AddDate(0, 1, 11)
duration := end.Sub(start)
fmt.Printf("Duration between %v and %v is %v", start, end, duration)
result :
> Duration between 2025-02-16 13:01:16.870769 +0100 CET m=+0.000735054 and 2025-03-27 13:01:16.870769 +0100 CET is 936h0m0s

Sub method returns Duration object has period between firs date and second date. if first date is before than second date, result would be negative

Convert date to timestamp by Golang

What is Unix timestamp ?

Unix timestamp is 64 bit value represents current time since 1 January 1970

Golang time.Now() returns current DateTime object

Getting timestamp in Golang
currentTime := time.Now()
fmt.Println(currentTime.Unix())

> 1738148999

UnixMilli
currentTime := time.Now()
fmt.Println(currentTime.UnixMilli())

> 1738149111065

UnixMicro
currentTime := time.Now()
fmt.Println(currentTime.UnixMicro())

> 1738149141680690

UnixNano
currentTime := time.Now()
fmt.Println(currentTime.UnixNano())

> 1738149218711926000

Tar create archive on command line

Tar is a command for compressing data as tar format

tar -cvf target-21-Jan-2025.tar.gz target-folder

-c helps to compress folder

-v is optional, if you want to verbose, you can add

-f <filename> Location of archive

Other options

-b block size : default 512 byte

-w Interactive mode

-z, -j, -J, --lzma Compress algorithm : gzip, bzip2, xz, lzma

Golang json Marshalling operations - convert object to json string

encoding/json package provides json operations.

Marshalling

Marshalling means converting object to json.

Marshalling array
myArray := []string{"test 1", "test 2", "test 3"}
jsonStr, err := json.Marshal(myArray)
fmt.Println(string(jsonStr), err)
result :

["test 1","test 2","test 3"] <nil>

Marshalling Map
myMap := make(map[string]interface{})
myMap["key 1"] = "val 1"
myMap["key 2"] = "val 2"
myMap["key int"] = 1234
myMap["key bool"] = true

jsonStr, err := json.Marshal(myMap)
fmt.Println(string(jsonStr), err)
result :

{"key 1":"val 1","key 2":"val 2","key bool":true,"key int":1234} <nil>

Marshalling Object
type TestObj struct {
	FieldStr             string
	FieldInt             int
	FieldMightNullString *string
	SubTestObj           *TestObj
}

testStr := "Might null field"
myTestObj := TestObj{
	FieldStr:             "test str",
	FieldInt:             1,
	FieldMightNullString: &testStr,
	SubTestObj: &TestObj{
		FieldStr: "Test str sub",
		FieldInt: 2},
}

jsonStr, err := json.Marshal(myTestObj)
fmt.Println(string(jsonStr), err)
result :

{"FieldStr":"test str","FieldInt":1,"FieldMightNullString":"Might null field","SubTestObj":{"FieldStr":"Test str sub","FieldInt":2,"FieldMightNullString":null,"SubTestObj":null}} <nil>

Marshalling as human readable text (json pretty print)

MarshalIndent function provides to generate pretty printed output

... 
jsonStr, err := json.MarshalIndent(myTestObj, "  ", "  ")
fmt.Println(string(jsonStr), err)
output :
{
    "FieldStr": "test str",
    "FieldInt": 1,
    "FieldMightNullString": "Might null field",
    "SubTestObj": {
      "FieldStr": "Test str sub",
      "FieldInt": 2,
      "FieldMightNullString": null,
      "SubTestObj": null
    }
  } <nil>

Golang server https certificate multi domain setup

loading certificate !

	rootMux := http.NewServeMux()
	cfg := &tls.Config{}

	cert, err := tls.LoadX509KeyPair("fullchain.pem", "/privkey.pem")
	if err != nil {
		panic(err)
	}

	cfg.Certificates = append(cfg.Certificates, cert)

	cert2, err2 := tls.LoadX509KeyPair("fullchain2.pem", "/privkey2.pem")
	if err2 != nil {
		panic(err2)
	}

	cfg.Certificates = append(cfg.Certificates, cert2)

	cfg.BuildNameToCertificate()

	server := http.Server{
		Addr:      ":443",
		Handler:   rootMux,
		TLSConfig: cfg,
	}

	err = server.ListenAndServeTLS("", "")
	if err != nil {
		panic(err)
	}

How to print Hex result on Linux command line !

How to export hex result on linux Console ?

There is a simple way to export hex from the file

xxd command helps for us

xxd -p file
ubuntu@instance-20240128-1936:~/go-blog-dbs$ xxd /etc/localtime 
00000000: 545a 6966 3200 0000 0000 0000 0000 0000  TZif2...........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0001 0000 0004 0000 0000  ................
00000030: 0000 5554 4300 545a 6966 3200 0000 0000  ..UTC.TZif2.....
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0001 0000  ................
00000060: 0004 0000 0000 0000 5554 4300 0a55 5443  ........UTC..UTC
00000070: 300a                                     0.