我正在try 将范围读取为json,但在执行json.unmarshal时我发现了一个问题.
这是一个测试代码-
import (
"encoding/json"
"testing"
"github.com/jackc/pgtype"
"github.com/stretchr/testify/assert"
)
type TestHealthPreference struct {
HealthRange pgtype.Int4range `json:"health_range"`
ID string `json:"id"`
}
// just a test to make sure unmarshaling works
func TestPreferenceUpdateUnmarshal(t *testing.T) {
jsonData := `{
"health_range": "[20,30)",
"id": "123"
}`
var update TestHealthPreference
err := json.Unmarshal([]byte(jsonData), &update)
if err != nil {
t.Errorf("Error while unmarshalling JSON: %v", err)
}
assert.Equal(t, 20, update.HealthRange.Lower)
}
这是个错误.
Error while unmarshalling JSON: json: cannot unmarshal string into Go struct field TestPreference.health_range of type pgtype.Int4range.
是否可以将其读作pgtype.Int4range?我猜这种类型只能用于数据库吧?FWIW,我正在使用PGx githorb.com/jackc/pgx/v4