Last active
June 28, 2021 12:32
-
-
Save mmuoDev/8704136050a21c2dc6dc65b84fdd9e42 to your computer and use it in GitHub Desktop.
Get latest date in a golang slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
dates := []string{"2021-01-20", "2021-03-05", "2021-02-25", "2021-03-04"} | |
latest, _ := time.Parse("2006-01-02", dates[0]) | |
for _, date := range dates { | |
dateValue, _ := time.Parse("2006-01-02", date) // convert 'String' to 'Time' data type | |
if d := dateValue.After(latest); d { | |
latest = dateValue | |
} | |
} | |
fmt.Println(latest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment