Skip to content

Instantly share code, notes, and snippets.

@guesslin
Created March 23, 2021 15:19
Show Gist options
  • Save guesslin/3f0125c88601ced9609ebb05b43ad93d to your computer and use it in GitHub Desktop.
Save guesslin/3f0125c88601ced9609ebb05b43ad93d to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"time"
)
func main() {
check(extractTime(nil))
check(extractTime(makeFields()))
}
func check(t time.Time, err error) {
if err != nil {
fmt.Println("Can't find time", err)
} else {
fmt.Printf("Time %v\n", t)
}
}
func extractTime(fields map[string]interface{}) (time.Time, error) {
timeField, ok := fields["time"].(string)
if !ok {
return time.Time{}, errors.New("Can't find time field")
}
return time.Parse(time.RFC3339, timeField)
}
func makeFields() map[string]interface{} {
m := make(map[string]interface{})
m["time"] = time.Now().UTC().Format(time.RFC3339)
return m
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment