These Go examples use Unix seconds for the portable baseline and show milliseconds only where the runtime exposes them naturally. The question to answer before copying a snippet is simple: what unit does the next API expect?
Get the current Unix time
seconds := time.Now().Unix()
milliseconds := time.Now().UnixMilli()
Convert a timestamp to UTC
t := time.Unix(1700000000, 0).UTC()
fmt.Println(t.Format(time.RFC3339))
Production note
time.Time represents an instant. Call UTC or load a named location deliberately before formatting it for people.
Keep the unit in the field name when a value crosses a boundary: createdAtSeconds and createdAtMs are longer than createdAt, but much less mysterious during an incident.
Frequent questions:
- Q: Should I store time.Time or Unix seconds?
- A: Use time.Time inside Go when you need calendar operations. Store Unix seconds or a database timestamp type when the value must be indexed, queried, or shared with other systems.
- Q: What unit does time.Unix expect?
- A: time.Unix expects seconds and nanoseconds as separate arguments. If you receive milliseconds, split or convert the value before calling time.Unix.
- Q: How do I verify Go output?
- A: Format the value with t.UTC().Format(time.RFC3339) and compare it with the timestamp expected by the API, queue, or database column you are writing.