dongshou6788 2015-06-07 11:28
浏览 3
已采纳

Golang-如何使单个结构与多个结构一起工作?

I want to make single struct works together with multiple structs.

In below codes, first query (rows) should be single struct because it return single row, while the second query (anotherquery) should be multiple struct because it return 5 rows.

Currently, what can I do is I make rows & anotherquery as multiple struct.

Below is server.go:

package main

import (
    "database/sql"
    "github.com/labstack/echo"
    _ "github.com/lib/pq"
    "html/template"
    "io"
    "log"
    "net/http"
)

type Gallery struct {
    Title, Content string
}
type Idcontainer struct {
    Stitle, Suri string
}

func main() {
    e := echo.New()

    e.Get("/post/:uritext", func(c *echo.Context) error {

        rows, err := db.Query("SELECT title, content FROM gallery WHERE uri=$1", c.Param("uritext"))
        anotherquery, err := db.Query("SELECT title AS stitle, uri AS suri FROM archive WHERE uri!=$1 LIMIT 5", c.Param("uritext"))

        gallery := []Gallery{}
        idcontainer := []Idcontainer{}

        for rows.Next() {
            g := Gallery{}
            err := rows.Scan(&g.Title, &g.Content)

            gallery = append(gallery, g)
        }

        for anotherquery.Next() {
            g := Idcontainer{}
            err := anotherquery.Scan(&g.Stitle, &g.Suri)

            idcontainer = append(idcontainer, g)
        }

        type Model struct {
            Galleries    []Gallery
            Idcontainers []Idcontainer
        }
        return c.Render(http.StatusOK, "onlytestingtpl", Model{
            Galleries:    gallery,
            Idcontainers: idcontainer,
        })
    })

    e.Run(":4444")
}

Template:

{{define "onlytestingtpl"}}
    {{.Title}}<br>
    {{.Content}}

    <h1>ID number:</h1>
    {{range .Idcontainers}}
    <a href='{{.Suri}}'>{{.Stitle}}</a>
    {{end}}
{{end}}
  • 写回答

1条回答 默认 最新

  • dtbc37573 2015-06-09 14:13
    关注

    I think the minimal change to your code to make this work would be the following.

    Change this:

        type Model struct {
            Galleries    []Gallery
            Idcontainers []Idcontainer
        }
        return c.Render(http.StatusOK, "onlytestingtpl", Model{
            Galleries:    gallery,
            Idcontainers: idcontainer,
        })
    

    To this:

        type Model struct {
            Gallery    Gallery // you said that theres only a single gallery right?
            Idcontainers []Idcontainer
        }
        return c.Render(http.StatusOK, "onlytestingtpl", Model{
            Gallery:    gallery[0],
            Idcontainers: idcontainer,
        })
    

    And then change your template to this:

    {{define "onlytestingtpl"}}
        {{.Gallery.Title}}<br>
        {{.Gallery.Content}}
    
        <h1>ID number:</h1>
        {{range .Idcontainers}}
        <a href='{{.Suri}}'>{{.Stitle}}</a>
        {{end}}
    {{end}}
    

    I've tried to mock up a simple example here: http://play.golang.org/p/uedcjXalEH

    Though you haven't asked for feedback on the code overall, I'll risk adding a few additional comments:

    • Consider using * (pointers) to objects rather than making copies each time.
    • Consider changing your initial rows to work something like this:

      gallery := Gallery{}
      if !rows.Next() {
          return rows.Err()
      }
      err := rows.Scan(&gallery.Title, &gallery.Content)
      
      // anotherquery remains the same ...
      
      return c.Render(http.StatusOK, "onlytestingtpl", Model{
          Gallery:    gallery,
          Idcontainers: idcontainer,
      })
      
    • This is probably just example code but in production you'll need more error handling of course :)

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致