Formatting a Time Stamp as a String in Go
When working with time-sensitive data in Go, it's often useful to format the current time as a string. This enables you to store and display the time in a human-readable and standardized format.
Solution Using time.Now() and time.Format():
The recommended approach in Go to obtain the current time and convert it to a string is to utilize the time.Now() function and the time.Format() method.
import "time" func main() { t := time.Now() fmt.Println(t.Format("20060102150405")) }
The above code will output a string representing the current time in the "YYYYMMDDhhmmss" format. This can be customized to any desired format using the pre-defined constants in the time package or by specifying a custom layout string.
Additional Options:
The above is the detailed content of How Can I Format a Go Time Stamp as a String?. For more information, please follow other related articles on the PHP Chinese website!