doupaoshu8334 2015-07-15 01:10
浏览 25
已采纳

我该如何清理这种返回类型,以应对新主义go-lang?

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!

  • 写回答

2条回答 默认 最新

  • doushenyi9104 2015-07-15 16:06
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)