Avatar ·

How to convert Unix timestamp to time.Time in Go?

Hello everyone! I just started learning Go and encountered a problem when working with time. I have an API that returns time in Unix timestamp format (e.g., 1696425600). I need to convert this number to a time.Time object so that I can format it for display to the user in a human-readable form, for example, "2023-10-04 15:20:00".

\n\n

I tried several approaches, but nothing works. Here's what I attempted:

\n\n
package main\n\nimport (\n    "fmt"\n    "time"\n)\n\nfunc main() {\n    timestamp := int64(1696425600)\n    \n    // First attempt - just assign\n    // t := time.Time(timestamp) // Error: cannot convert timestamp to time.Time\n    \n    // Second attempt - via Parse\n    // t, _ := time.Parse("Unix time