denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.81k stars 493 forks source link

How to cast mssql.UniqueIdentifier to uuid inside struct #732

Open kzmKZuzmo opened 2 years ago

kzmKZuzmo commented 2 years ago

Hello I have the struct like here named person and when I call my rest endpoint and get data from mssql database (where ID is ID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() )

then golang return response with array of number

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 }

bad response after call like api from I get { "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" }

I would like have response like

{ "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.Find(&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)

} `

where I have a bug?