Unix Timestamps with Go

Unix timestamps are very easy to use and straightforward to implement in Go.

Current Unix timestamp (in seconds) with Go

             
            package main

            import (
                "fmt"
                "time"
            )
                
            func main() {
                var ts = time.Now().Unix()
                fmt.Println(ts)
            }
                
            
        
Run this code online.

Date to Unix timestamp with Go

             
            package main

            import (
                "fmt"
                "time"
            )
                
            func main() {
                timestring := "01/02/2006 3:04:05 PM"
                time, err := time.Parse(timestring, "02/28/2016 9:03:46 PM")
                if err != nil {
                    fmt.Println(err)
                }
                fmt.Println(time.Unix())
            }    
            
        
Run this code online.

Convert Unix timestamp to date with Go

            
            package main

            import (
                "fmt"
                "time"
            )
                
            func main() {
                var timestamp int64 = 1714862093
                
                t := time.Unix(timestamp, 0)
                fmt.Println(t.UTC())
                
            }
                
            
        
Run this code online.