我很好奇,爲什麼這DeepEqual支票是假的:Golang:reflect.DeepEqual返回false
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
)
type Result struct {
Topic string `json:"topic,omitempty"`
Id int `json:"id,omitempty"`
}
// Result represents the returned collection from a topic search.
type ResultResponse struct {
Result []Result `json:"results"`
}
func main() {
want := ResultResponse{
[]Result{{Topic: "Clojure", Id: 1000}},
}
input := `{"results": [ {"topic": "Clojure", "id": 1000} ]}`
p := ResultResponse{}
err := json.NewDecoder(strings.NewReader(input)).Decode(&p)
if err != nil {
panic(err)
}
fmt.Println(p, want)
if !reflect.DeepEqual(input, want) {
log.Printf("returned %+v, want %+v", p, want)
}
}
這是由於一個錯字:'如果嘗試!反映.DeepEqual(p,want)'。例如。 https://play.golang.org/p/LkYrawkjcU – 2015-04-02 21:41:15