0

This model I am trying to make with neoism is working now but I can't figure out how to make the return type shorter and or cleaner looking. There must be a way to make this: []struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" }

Into: []Account or something

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
}

Here is another copy: http://play.golang.org/p/14gzc0a_89

I am new to go and I am going off of these examples for neoism: https://gist.github.com/verdverm/b43444063995a7f5b913

There are many examples, but verdverm is always just outputting and not returning.

I have a controller as well where I would like to output the 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()
}

Thanks for any help/tips for go newbie!

4

2 回答 2

0

getAccounts 可以返回一个帐户指针数组吗?

func GetAccounts() []*Account {}

于 2015-07-15T01:18:11.147 回答
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的指针来响应neoism db。

于 2015-07-15T16:06:22.663 回答