Use the time.Now().Unix() function to get the current timestamp in Golang. The time.Unix() function is a method of the time.Time type in Go, which represents a time value.
The time.Unix() function returns the Unix time, which is the number of seconds elapsed since January 1, 1970, UTC, and the time.Now() function returns a time.Time type, which contains both the date and time.
The time package in Go provides functions to work with dates and times and the time.Now() function returns the current local time.
If you need only the timestamp, use the Unix() or UnixNano() methods to get the number of seconds or nanoseconds since the Unix epoch (January 1, 1970).
Example
package main
import (
"fmt"
"time"
)
func main() {
timestamp := time.Now().Unix()
fmt.Println("The current timestamp is:", timestamp)
}
Output
The current timestamp is: 1676117332
You can see that we got the current timestamp in seconds using the time.Now().Unix() function.
How to get the current time in Go
If you want to get the current time in Golang, you can use the time.Now() function like this: time.Now().
package main
import (
"fmt"
"time"
)
func main() {
tme := time.Now()
fmt.Println("The current time is:", tme)
}
Output
The current time is: 2023-02-11 17:48:33.002301 +0530 IST m=+0.000221043
Conclusion
To get the “timestamp” of a date in (number of seconds from January 1970), the correct function is Time.Unix() function in Golang.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.