You could do it by slicing your input string, and assembling the parts in different order:
func transform(s string) string {
d, m, y := s[:2], s[3:5], s[6:]
return y + "-" + m + "-" + d
}
Note: the above function does not validate the input, it could panic if the input is shorter than 6 bytes.
If you need input validation (including date validation), you could use the time package to parse the date, and format it into your expected output:
func transform2(s string) (string, error) {
t, err := time.Parse("02.01.2006", s)
if err != nil {
return "", err
}
return t.Format("2006-01-02"), nil
}
Testing the above functions:
fmt.Println(transform("31.12.2019"))
fmt.Println(transform2("31.12.2019"))
Output (try it on the Go Playground):
2019-12-31
2019-12-31 <nil>