【发布时间】:2014-07-08 21:53:57
【问题描述】:
这个问题跟随another question of mine。
在以下测试代码中尝试将 res 转换为 ListSociete 时,我并没有完全理解有什么问题:
import (
"errors"
"fmt"
"github.com/jmcvetta/neoism"
)
type Societe struct {
Name string
}
type ListSociete []Societe
func loadListSociete(name string) (ListSociete, error) {
db, err := neoism.Connect("http://localhost:7474/db/data")
if err != nil {
return nil, err
}
res := []struct {
Name string `json:"a.name"`
}{}
cq := neoism.CypherQuery{
Statement: `
MATCH (a:Societe)
WHERE a.name = {name}
RETURN a.name
`,
Parameters: neoism.Props{"name": name},
Result: &res,
}
db.Cypher(&cq)
if len(res) == 0 {
return nil, errors.New("Page duz not exists")
}
r := res[0]
return ListSociete(res), nil
}
[]struct{Name string} 与 []struct{Name string json:"a.name" } 不同吗?
还是 ListSociete 与 []struct{Name string} 不同?
谢谢。
【问题讨论】:
-
将
json:"a.name"添加到Societe是否有效?使用res := Societe{}还有什么问题? -
错误是什么?输出?
-
对不起,我忘记了。这是:
cannot convert res (type []struct { Name string "json:\"a.name\"" }) to type ListSociete -
@OneOfOne 做了一些测试,将 json 标签添加到 Societe 不起作用。 ListSociete 似乎与 []struct{Name string} 不同。
-
在定义
res := Societe{}时,这并不是一个真正的选择,因为我需要标签来从我的数据库查询中提取结果(此外,Societe 只是一个结构,而 res 是一个结构的片段)
标签: struct go type-conversion