我试图用新主义制作这个模型现在正在运作,但我无法弄清楚如何使返回类型更短或更清晰。
必须有办法做到这一点:
[]struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" }
分为:
[]Account
或其他什么
package models
import (
"errors"
"strconv"
"time"
// "fmt"
"github.com/jmcvetta/neoism"
)
var (
Accounts map[string]*Account
)
var (
db *neoism.Database
)
func panicErr(err error) {
if err != nil {
panic(err)
}
}
type Account struct {
UserId string `json:"account.UserId"`
FirstName string `json:"account.firstName"`
LastName string `json:"account.lastName"`
}
func init() {
var err error
db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
if err != nil {
panic(err)
}
}
// map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]
func GetAccounts() []struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" } {
stmt := `
MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
`
res := []struct {
UserId string `json:"account.UserId"`
FirstName string `json:"account.firstName"`
LastName string `json:"account.lastName"`
}{}
// construct query
cq := neoism.CypherQuery{
Statement: stmt,
Result: &res,
}
// execute query
err := db.Cypher(&cq)
panicErr(err)
return res
}
这是另一个副本: http://play.golang.org/p/14gzc0a_89
我是新手,我将从这些新例子的例子出发: https://gist.github.com/verdverm/b43444063995a7f5b913
有很多例子,但是verdverm总是只输出而不是返回。
我还有一个控制器,我想输出json。
package controllers
import (
"github.com/astaxie/beego"
"social/models"
)
type AccountsController struct {
beego.Controller
}
func (c *AccountsController) Get() {
c.Data["json"] = models.GetAccounts()
// c.Data["json"] = "hello world"
c.ServeJson()
}
感谢有关新手的任何帮助/提示!
答案 0 :(得分:0)
package models
import (
"errors"
"strconv"
"time"
// "fmt"
"github.com/jmcvetta/neoism"
)
var (
Accounts map[string]*Account
)
var (
db *neoism.Database
)
func panicErr(err error) {
if err != nil {
panic(err)
}
}
type Account struct {
UserId string `json:"account.UserId"`
FirstName string `json:"account.firstName"`
LastName string `json:"account.lastName"`
}
func init() {
var err error
db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
if err != nil {
panic(err)
}
}
// map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]
func GetAccounts() []Account {
stmt := `
MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
`
var accounts []Account
// construct query
cq := neoism.CypherQuery{
Statement: stmt,
Result: &accounts,
}
// execute query
err := db.Cypher(&cq)
panicErr(err)
return accounts
}
需要在函数上方使用Account结构,然后在里面创建一个变量,并使用指向var的新事件db的响应。