Home >Backend Development >Golang >How Do I Get a Unix Timestamp in Go?

How Do I Get a Unix Timestamp in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-12-03 06:05:10502browse

How Do I Get a Unix Timestamp in Go?

Getting a Unix Timestamp in Go: Current Time in Seconds Since Epoch

In earlier versions of Go, such as r60, obtaining a Unix timestamp (the current time in seconds since the epoch) could be achieved with code like this:

if t, _, err := os.Time(); err == nil {
   port[5] = int32(t)
}

However, with newer weekly builds, this approach is outdated. To update the code for current Go implementations:

  • Import the "time" package: This package provides functions for working with time and dates.
  • Use time.Now().Unix(): Replace the original time retrieval line with this function call. It returns the current time in seconds since the epoch.

Here's the updated code:

import "time"

// ...

port[5] = time.Now().Unix()

This code will now correctly obtain the current Unix timestamp, ensuring compatibility with the latest Go builds.

The above is the detailed content of How Do I Get a Unix Timestamp in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn