func MustJSONMarshall(t *testing.T, a interface{}) []byte { res, err := json.Marshal(a) if err != nil { t.FatalF("err: %s", err) } return res } func TestWhatver(t *testing.T) { elem := MyStruct{Field:"something"} elemB := MustJSONMarshall(t, elem) ... }