Home >Backend Development >Golang >How to Parse Time Strings in Go with Unfamiliar Formats?
Understanding Time String Parsing in Go
Parsing a string into a time.Time value in Go requires specifying the correct format of the string. When faced with an unfamiliar format like "20171023T183552", it's unclear how to proceed.
Fortunately, Go allows for flexible formatting by creating a custom layout string that matches the input string's structure. To parse the given string, we can use the layout "20060102T150405" which corresponds to the format: "YYYYMMDDTHHmmSS".
import ( "fmt" "time" ) func main() { s := "20171023T183552" t, err := time.Parse("20060102T150405", s) fmt.Println(t, err) }
This code will output:
2017-10-23 18:35:52 +0000 UTC <nil>
This demonstrates that even for unfamiliar formats, we can define our own layouts to successfully parse strings into time.Time values.
The above is the detailed content of How to Parse Time Strings in Go with Unfamiliar Formats?. For more information, please follow other related articles on the PHP Chinese website!