Created
March 23, 2021 15:19
-
-
Save guesslin/3f0125c88601ced9609ebb05b43ad93d to your computer and use it in GitHub Desktop.
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
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