Go gotcha: Multiple values in single value context?

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.

Use the blank identifier (underscore) for return values you're not interested in:

t, _ := time.Parse(time.RFC822, "04 Nov 17 12:54 CET")
fmt.Println(t)
2017-11-04 12:54:00 +0000 CET

Comments

Be the first to comment!