你好,我有像这里名为 person 的结构,当我调用我的休息端点并从 mssql 数据库获取数据时(其中 ID 是
ID 唯一标识符 默认 NEWSEQUENTIALID() )
package entities import ( mssql "github.com/denisenkom/go-mssqldb" "time" )
type Person struct {
ID mssql.UniqueIdentifier
FullName string Comments
string DateCreated time.Time
DateModified time.Time
}
然后 golang 返回带有数字数组的响应
当我将 ID 更改为字符串时,它有不可读的字符。
反应不好
像 api 这样的调用之后我得到
{ "ID": [ 160,63, 67,62,243,107,20,16,143,174,0, 45,192,113,147,0 ],
"FullName": "Tony Stark",
"Comments": "",
"DateCreated": "2022-04-06T10:12:18.523Z",
"DateModified": "2022-04-06T10:12:18.523Z"
}
我想要这样的回应
{ "ID": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
"FullName": "Tony Stark",
"Comments": "",
"DateCreated": "2022-04-06T10:12:18.523Z",
"DateModified": "2022-04-06T10:12:18.523Z" }
func Getperson(w http.ResponseWriter, r *http.Request) {
var persons []entities.person
database.Instance.Raw("SELECT * FROM Persons").Scan(&person)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(person)
}
func Connect(connectionString string) {
//dsn := "sqlserver://sa:*****@127.0.0.1:1433?database=mydbforgo"
Instance, err = gorm.Open(sqlserver.Open(connectionString), &gorm.Config{})
if err != nil {
log.Fatal(err)
panic("Cannot connect to DB")
}
log.Println("Connected to Database...")
}
我哪里有bug?