dougou8458 2014-08-08 10:08
浏览 61
已采纳

对指针和值参数新概念感到困惑

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

However, look at the following code snippet.

db, _ := neoism.Connect("http://localhost:7474/db/data")
// Create a node with a Cypher quer
// Issue a query
//
res1 := []struct {
    A string `json:"n.email"`
}{}
cq1 := neoism.CypherQuery{
    //Use backticks for long statements - Cypher is whitespace indifferent
    Statement: `
        MATCH (n:Account {email: {email}})
        RETURN n.email
    `,
    Parameters: neoism.Props{"email": "hans@ueli.com"},
    Result:     &res1,
}
db.Cypher(&cq1)
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.

// Validate, if email already available in db
res2 := []*struct {
    A string `json:"n.email"`
}{}
cq := &neoism.CypherQuery{
    Statement: `
        MATCH (n:Account {email: {email}})
        RETURN n.email
    `,
    Parameters: neoism.Props{"email": "hans@ueli.com"},
    Result:     res2,
}
db.Cypher(cq)
fmt.Println(res2)

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

[{hans@ueli.com}]
[]

What do I wrong with pointer res2 here?

  • 写回答

1条回答 默认 最新

  • dongranding3909 2014-08-08 10:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集