Skip to content

Instantly share code, notes, and snippets.

@icio icio/ignore_test.go

Last active Dec 8, 2017
What would you like to do?
Example of using sentinel values with
package example_test
import (
type Object struct {
String string `json:"string,omitempty"`
Int int `json:"int,omitempty"`
Object *Object `json:"object,omitempty"`
// Handler writes a {"string": "a", "object": {"string": "b"}} in response.
func Handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
String: "A",
Object: &Object{
String: "B",
Object: &Object{
String: "C",
var (
// ignore is our sentinel value for ignoring map fields.
ignore = struct{}{}
// ignoreOpt is how we tell cmp.Diff and cmp.Equal to discount ignore values.
ignoreOpt = cmp.FilterValues(
func(a, b interface{}) bool {
return a == ignore || b == ignore
// TestHandler demonstrates comparing interface{} values which we might do if
// we're checking the contents of a JSON body.
func TestHandler(t *testing.T) {
var actual interface{}
// Get the parsed value from the handler response.
r := httptest.NewRecorder()
Handler(r, httptest.NewRequest("POST", "/", nil))
// Our expected template.
expected := map[string]interface{}{
"string": "A",
"object": map[string]interface{}{
"string": ignore,
"object": map[string]interface{}{
"string": "C",
if d := cmp.Diff(expected, actual, ignoreOpt); d != "" {
t.Errorf("Unexpected diff:\n%s", d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.