Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created January 9, 2017 18:55
Show Gist options
  • Save aalvesjr/04b072071806e875a2230485dd68334f to your computer and use it in GitHub Desktop.
Save aalvesjr/04b072071806e875a2230485dd68334f to your computer and use it in GitHub Desktop.
Testing errors for API in golang
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string
Errors map[string][]string `json:"-"`
}
func main() {
p := Person{"", map[string][]string{}}
// simulating the p.Validate() where the Errors array should be filled
p.Errors["name"] = append(p.Errors["name"], "blank")
p.Errors["name"] = append(p.Errors["name"], "invalid")
j, _ := json.Marshal(p)
e, _ := json.Marshal(p.Errors)
fmt.Println("resource:", string(j), ", errors:", string(e))
}
// => resource: {"Name":""} , errors: {"name":["blank","invalid"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment