如何清理neoism go-lang的返回类型?

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

How can I clean up this return type for neoism go-lang?

问题

我正在尝试使用neoism创建的这个模型现在正在工作,但我无法弄清楚如何使返回类型更短或更清晰。

可能有一种方法可以将这个:
[]struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" }

变成:
[]Account 或其他什么东西

以下是要翻译的代码:

package models

import (
  "errors"
  "strconv"
  "time"

  "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)
  }
}

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"`
  }{}

  cq := neoism.CypherQuery{
    Statement:  stmt,
    Result:     &res,
  }

  err := db.Cypher(&cq)
  panicErr(err)

  return res
}

以下是另一个副本:
http://play.golang.org/p/14gzc0a_89

我是Go的新手,我正在根据这些neoism的示例进行开发:
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.ServeJson()
}

希望对Go的新手有所帮助!

英文:

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!

答案1

得分: 0

GetAccounts()函数能够返回一个账户指针的数组吗?

func GetAccounts() []*Account {}

英文:

Can getAccounts return an array of account pointers?

func GetAccounts() []*Account {}

答案2

得分: 0

包模型

导入 (
"错误"
"strconv"
"时间"
// "fmt"

"github.com/jmcvetta/neoism"
)

var (
账户映射[string]*账户
)

var (
数据库 *neoism.数据库
)

func 报错(err 错误) {
如果 err != nil {
报错(err)
}
}

类型 账户 结构体 {
用户ID 字符串 json:"account.UserId"
名字 字符串 json:"account.firstName"
姓氏 字符串 json:"account.lastName"
}

初始化() {
var err 错误
数据库, err = neoism.连接("http://neo4j:password@localhost:7474/db/data")
如果 err != nil {
报错(err)
}
}

// map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]

获取账户() []账户 {
语句 := MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName

var 账户们 []账户

// 构建查询
cq := neoism.CypherQuery{
语句: 语句,
结果: &账户们,
}

// 执行查询
错误 := 数据库.Cypher(&cq)
报错(错误)

返回 账户们
}

在函数之前使用上面的账户结构体,然后在内部创建一个变量,并使用指针将其用于neoism数据库的响应。

英文:
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
}

need to use the Account struct above the function and then make a variable inside and use pointer to var for the response of neoism db.

huangapple
  • 本文由 发表于 2015年7月15日 09:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/31419861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定