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

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

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 或其他什么东西

以下是要翻译的代码:

  1. package models
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "github.com/jmcvetta/neoism"
  7. )
  8. var (
  9. Accounts map[string]*Account
  10. )
  11. var (
  12. db *neoism.Database
  13. )
  14. func panicErr(err error) {
  15. if err != nil {
  16. panic(err)
  17. }
  18. }
  19. type Account struct {
  20. UserId string `json:"account.UserId"`
  21. FirstName string `json:"account.firstName"`
  22. LastName string `json:"account.lastName"`
  23. }
  24. func init() {
  25. var err error
  26. db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
  27. if err != nil {
  28. panic(err)
  29. }
  30. }
  31. func GetAccounts() []struct {
  32. UserId string `json:"account.UserId"`
  33. FirstName string `json:"account.firstName"`
  34. LastName string `json:"account.lastName"`
  35. } {
  36. stmt := `
  37. MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
  38. `
  39. res := []struct {
  40. UserId string `json:"account.UserId"`
  41. FirstName string `json:"account.firstName"`
  42. LastName string `json:"account.lastName"`
  43. }{}
  44. cq := neoism.CypherQuery{
  45. Statement: stmt,
  46. Result: &res,
  47. }
  48. err := db.Cypher(&cq)
  49. panicErr(err)
  50. return res
  51. }

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

我是Go的新手,我正在根据这些neoism的示例进行开发:
https://gist.github.com/verdverm/b43444063995a7f5b913

有很多示例,但verdverm只是输出而不是返回。

我还有一个控制器,我想在其中输出JSON。

  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "social/models"
  5. )
  6. type AccountsController struct {
  7. beego.Controller
  8. }
  9. func (c *AccountsController) Get() {
  10. c.Data["json"] = models.GetAccounts()
  11. c.ServeJson()
  12. }

希望对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

  1. package models
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. // "fmt"
  7. "github.com/jmcvetta/neoism"
  8. )
  9. var (
  10. Accounts map[string]*Account
  11. )
  12. var (
  13. db *neoism.Database
  14. )
  15. func panicErr(err error) {
  16. if err != nil {
  17. panic(err)
  18. }
  19. }
  20. type Account struct {
  21. UserId string `json:"account.UserId"`
  22. FirstName string `json:"account.firstName"`
  23. LastName string `json:"account.lastName"`
  24. }
  25. func init() {
  26. var err error
  27. db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
  28. if err != nil {
  29. panic(err)
  30. }
  31. }
  32. // map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]
  33. func GetAccounts() []struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" } {
  34. stmt := `
  35. MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
  36. `
  37. res := []struct {
  38. UserId string `json:"account.UserId"`
  39. FirstName string `json:"account.firstName"`
  40. LastName string `json:"account.lastName"`
  41. }{}
  42. // construct query
  43. cq := neoism.CypherQuery{
  44. Statement: stmt,
  45. Result: &res,
  46. }
  47. // execute query
  48. err := db.Cypher(&cq)
  49. panicErr(err)
  50. return res
  51. }

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.

  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "social/models"
  5. )
  6. type AccountsController struct {
  7. beego.Controller
  8. }
  9. func (c *AccountsController) Get() {
  10. c.Data["json"] = models.GetAccounts()
  11. // c.Data["json"] = "hello world"
  12. c.ServeJson()
  13. }

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数据库的响应。

英文:
  1. package models
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. // "fmt"
  7. "github.com/jmcvetta/neoism"
  8. )
  9. var (
  10. Accounts map[string]*Account
  11. )
  12. var (
  13. db *neoism.Database
  14. )
  15. func panicErr(err error) {
  16. if err != nil {
  17. panic(err)
  18. }
  19. }
  20. type Account struct {
  21. UserId string `json:"account.UserId"`
  22. FirstName string `json:"account.firstName"`
  23. LastName string `json:"account.lastName"`
  24. }
  25. func init() {
  26. var err error
  27. db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
  28. if err != nil {
  29. panic(err)
  30. }
  31. }
  32. // map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]
  33. func GetAccounts() []Account {
  34. stmt := `
  35. MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
  36. `
  37. var accounts []Account
  38. // construct query
  39. cq := neoism.CypherQuery{
  40. Statement: stmt,
  41. Result: &accounts,
  42. }
  43. // execute query
  44. err := db.Cypher(&cq)
  45. panicErr(err)
  46. return accounts
  47. }

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:

确定