Multiple values in single value context

yourbasic.org/golang

Why doesn’t this code compile?

t := time.Parse(time.RFC822, "04 Nov 17 12:54 CET")
fmt.Println(t)
../main.go:1: multiple-value time.Parse() in single-value context

Answer

The time.Parse function returns two values, a time.Time and an error, and you must use both.

t, err := time.Parse(time.RFC822, "04 Nov 17 12:54 CET")
if err != nil {
    // TODO: Handle error.
}
fmt.Println(t)
2017-11-04 12:54:00 +0000 CET

Share this page: