如何在Go中将结构指针转换为类型指针列表

huangapple go评论113阅读模式
英文:

how to convert a struct pointer to list of type pointers in go

问题

我有一个类似的结构体:

  1. type inv struct {
  2. ID int `json:"id"`
  3. Name string `json:"name"`
  4. }

我正在从数据库查询一些数据(假设没有错误):

  1. rows, err := db.Query("select id, name from inv_table")

通常情况下,我需要通过扫描来从行中提取数据:

  1. var i inv
  2. for rows.Next() {
  3. rows.Scan(&i.ID, &i.Name)
  4. }

我认为这可能会起作用(明天要测试一下):

  1. var i inv
  2. for rows.Next() {
  3. var x []interface{} = []interface{}{&i.ID, &i.Name}
  4. rows.Scan(x...)
  5. }

实际上,查询结果集中还有更多的列。

  1. rows.Scan(&i)

或者至少:

  1. rows.Scan(getExportedValuePointers(&i)...)

我想我可以编写一些类似于encode/xml或encode/json的反射代码,但我希望有人已经做过了。

更新:以下代码按预期工作,但不是我想要的:

  1. package main
  2. import "fmt"
  3. type inv struct {
  4. A int
  5. B string
  6. }
  7. func main() {
  8. var i inv
  9. fmt.Printf("hello\n")
  10. n, err := fmt.Sscan("1 c", &i.A, &i.B)
  11. fmt.Printf("retval: %d %#v\n", n, err)
  12. fmt.Printf("retval: %d, %s, %d %#v\n", i.A, i.B, n, err)
  13. j := []interface{}{&i.A, &i.B}
  14. k := []interface{}{i.A, i.B}
  15. n, err = fmt.Sscan("2 d", j... )
  16. fmt.Printf("retval: %d, %s, %d %#v\n", i.A, i.B, n, err)
  17. fmt.Printf("retval: %d, %s\n", k... )
  18. }
英文:

I have a struct that looks like:

  1. type inv struct {
  2. ID int `json:"id"`
  3. Name string `json:"name"`
  4. }

I'm querying some data from the database(assuming there are no errors):

  1. rows, err := db.Query("select id, name from inv_table")

Normally, I'd have to pull the data from the row by scanning

  1. var i inv
  2. for rows.Next() {
  3. rows.Scan(&i.ID, &i.Name)
  4. }

I think this might work (to be tested tomorrow):

  1. var i inv
  2. for rows.Next() {
  3. var x []interface{} = [&i.ID, &i.Name]
  4. rows.Scan(x... )
  5. }

In actuality I have many more columns in the result set from the query.

  1. rows.Scan(&i)

or at least:

  1. rows.Scan(getExportedValuePointers(&i)... )

I suppose I could always write some sort of encoder, however, it seems to me that there should be something in the box already.

** I realize that I could always write some reflection code similar to the encode/xml or encode/json ... but I'm hoping that someone has already done it.

UPDATE: The following code works as expected but not as I want:

  1. package main
  2. import "fmt"
  3. type inv struct {
  4. A int
  5. B string
  6. }
  7. func main() {
  8. var i inv
  9. fmt.Printf("hello\n")
  10. n, err := fmt.Sscan("1 c", &i.A, &i.B)
  11. fmt.Printf("retval: %d %#v\n", n, err)
  12. fmt.Printf("retval: %d, %s, %d %#v\n", i.A, i.B, n, err)
  13. j := []interface{}{&i.A, &i.B}
  14. k := []interface{}{i.A, i.B}
  15. n, err = fmt.Sscan("2 d", j... )
  16. fmt.Printf("retval: %d, %s, %d %#v\n", i.A, i.B, n, err)
  17. fmt.Printf("retval: %d, %s\n", k... )
  18. }

答案1

得分: 2

如果你正在寻找一个可以自动将你的SQL查询绑定到你的"inv"结构体的包,那么可以看一下gorp:

https://github.com/coopernurse/gorp

英文:

If you're looking for a package that will automatically bind your SQL query to your "inv" struct then take a look at gorp:

https://github.com/coopernurse/gorp

答案2

得分: 0

没有自动完成这个任务的方法,但是你可以使用反射包(reflect package)编写一个函数来实现。

英文:

There is nothing to do this automatically, but you could write a function using the reflect package to do it.

huangapple
  • 本文由 发表于 2012年12月11日 10:12:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/13812560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定