英文:
Data binding in go using sqlBoiler
问题
以下是翻译好的内容:
我正在从MYSQL数据库中获取一些数据.. 使用查询数据是正确的(例如10行)
我想将其绑定到一个模型列表中进行显示。
但是出现了恐慌错误
type UserDetails []UserDetail
type UserDetail struct {
id string `json:"id" boil:",bind"`
ScreenName string `json:"screenName" boil:",bind" `
}
func (m *mysqlStore) GetUsersDetails(ctx context.Context) () {
var userDetails []*models.UserDetail
err := queries.Raw(`
SELECT
user.id,
user.screen_name
FROM user
group by user.id
`).Bind(ctx, m.db, &userDetails)
if err != nil {
fmt.Println(err)
}
fmt.Println(userDetails)
}
在这里,我使用MYSQLQuery获取了正确的数据。我想以数组列表的形式显示出来,例如:
[
{"id":"1",
"screenName":"test"},
{"id":"2",
"screenName":"test"}
]
我的Go代码有什么问题?
英文:
I am fetching some data from MYSQL database.. Using query data is getting correclty (eg 10 rows)
I want to bind into a list of model for displaying.
But panic error displaying
type UserDetails []UserDetail
type UserDetail struct {
id string `json:"id" boil:",bind"`
ScreenName string `json:"screenName" boil:",bind" `
}
func (m *mysqlStore) GetUsersDetails(ctx context.Context) () {
var userDetails []*models.UserDetail
err := queries.Raw(`
SELECT
user.id,
user.screen_name
FROM user
group by user.id
`).Bind(ctx, m.db, &userDetails)
if err != nil {
fmt.Println(err)
}
fmt.Println(userDetails)
}
here using the MYSQLQuery i am getting the correct data. I want to display that in a list of arrary eg:
[
{"id":"1",
"screenName":"test"},
{"id":"2",
"screenName":"test"}
]
what is the issue in my go code?
答案1
得分: 0
我得到了答案
在这种情况下,结构体应该是这样的:
type UserDetail struct {
id string `json:"id"`
ScreenName string `json:"screenName"`
}
以及
var userDetails []models.UserDetail
英文:
I got the Answer
In this case struct must be
type UserDetail struct {
id string `json:"id"`
ScreenName string `json:"screenName"`
}
and
var userDetails []models.UserDetail
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论