登录
首页 >  Golang >  Go问答

Go语言中使用GORM库处理msSQL的UNIQUEIDENTIFIER结构

来源:stackoverflow

时间:2024-02-16 18:15:23 210浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《Go语言中使用GORM库处理msSQL的UNIQUEIDENTIFIER结构》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

您好,我有这样的结构,名为 person,当我调用休息端点并从 mssql 数据库获取数据时(其中 id 是 id uniqueidentifier default 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?


正确答案


您实际上需要对该值调用 .String (例如:someStruct.uniqueID.String())以获得 GUID 的字符串表示形式:https://github.com/denisenkom/go-mssqldb/blob/master/uniqueidentifier.go#L72-L80

以上就是《Go语言中使用GORM库处理msSQL的UNIQUEIDENTIFIER结构》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>