关于指针和值参数的困惑

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

Confused about pointer and value parameter neoism

问题

我正在使用Go编写一个Web应用程序,并使用Neo4j数据库存储数据。作为Neo4j到Go的API,我选择了[neoism][1]。

然而,请看下面的代码片段。

  1. db, _ := neoism.Connect("http://localhost:7474/db/data")
  2. // 创建一个带有Cypher查询的节点
  3. // 发出一个查询
  4. res1 := []struct {
  5. A string `json:"n.email"`
  6. }{}
  7. cq1 := neoism.CypherQuery{
  8. // 对于长语句,请使用反引号- Cypher不关心空格
  9. Statement: `
  10. MATCH (n:Account {email: {email}})
  11. RETURN n.email
  12. `,
  13. Parameters: neoism.Props{"email": "hans@ueli.com"},
  14. Result: &res1,
  15. }
  16. db.Cypher(&cq1)
  17. fmt.Println(res1)

我在这里查询了Account节点的数据,并得到了一个返回结果,一切正常。

第二段代码几乎相同,但我在这里直接创建了一个指针切片(变量res2)。

  1. // 验证数据库中是否已经存在该电子邮件
  2. res2 := []*struct {
  3. A string `json:"n.email"`
  4. }{}
  5. cq := &neoism.CypherQuery{
  6. Statement: `
  7. MATCH (n:Account {email: {email}})
  8. RETURN n.email
  9. `,
  10. Parameters: neoism.Props{"email": "hans@ueli.com"},
  11. Result: res2,
  12. }
  13. db.Cypher(cq)
  14. fmt.Println(res2)

它们之间的区别是,第一个示例中我得到了一个结果,但第二个示例没有。

结果:

  1. [{hans@ueli.com}]
  2. []

我在这里对指针res2做错了什么?

英文:

I am writing a web application in Go and use Neo4j database for storing data. As Neo4j api to Go, i choose [neoism][1].

However, look at the following code snippet.

  1. db, _ := neoism.Connect("http://localhost:7474/db/data")
  2. // Create a node with a Cypher quer
  3. // Issue a query
  4. //
  5. res1 := []struct {
  6. A string `json:"n.email"`
  7. }{}
  8. cq1 := neoism.CypherQuery{
  9. //Use backticks for long statements - Cypher is whitespace indifferent
  10. Statement: `
  11. MATCH (n:Account {email: {email}})
  12. RETURN n.email
  13. `,
  14. Parameters: neoism.Props{"email": "hans@ueli.com"},
  15. Result: &res1,
  16. }
  17. db.Cypher(&cq1)
  18. fmt.Println(res1)

I query here data from node Account and got a result return, everything works fine here.
The second code almost the same, but I am creating here directly(variable res2) a pointer slice.

  1. // Validate, if email already available in db
  2. res2 := []*struct {
  3. A string `json:"n.email"`
  4. }{}
  5. cq := &neoism.CypherQuery{
  6. Statement: `
  7. MATCH (n:Account {email: {email}})
  8. RETURN n.email
  9. `,
  10. Parameters: neoism.Props{"email": "hans@ueli.com"},
  11. Result: res2,
  12. }
  13. db.Cypher(cq)
  14. fmt.Println(res2)

The difference between them are, I've got by the first sample a result but second not.
Result:

  1. [{hans@ueli.com}]
  2. []

What do I wrong with pointer res2 here?
[1]: https://github.com/jmcvetta/neoism

答案1

得分: 2

从neoism文档中:

> 结果必须是指向结构体切片的指针 - 例如 &[]someStruct{}

关于结构体指针的切片没有提到任何内容,所以我认为你的切片是空的,因为函数不期望指针,所以它无法将任何内容放入切片中。

当我给sqlx.Query错误类型的切片时,我遇到了相同的行为。一开始缺乏错误信息确实令人沮丧,但很快就变成了一种习惯性反应。

英文:

From the neoism documentation:

> Result must be a pointer to a slice of structs - e.g. &[]someStruct{}

Nothing is said about slices of struct pointers, so I assume that your slice is empty because the function is not expecting pointers, so it couldn't put anything in the slice.

I encountered the same behavior when giving sqlx.Query the wrong type of slice. The lacks of error is quite frustrating the first times, but it quickly becomes a reflex.

huangapple
  • 本文由 发表于 2014年8月8日 18:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/25201001.html
匿名

发表评论

匿名网友

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

确定