关于指针和值参数的困惑

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

Confused about pointer and value parameter neoism

问题

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

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

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

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

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

// 验证数据库中是否已经存在该电子邮件
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)

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

结果:

[{hans@ueli.com}]
[]

我在这里对指针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.

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]: 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:

确定