去郎增量号

时间:2017-10-16 09:16:24

标签: postgresql go

我无法在桌子上增加行数。
我使用了revel框架,并尝试使用简单的crud。

问题是:"如何用Golang显示增量编号。?"。

这是我的控制器代码 book.go
此控制器显示来自postgresql数据库的数据。

func allBooks() ([]models.Book, error) {
    //Retrieve
    books := []models.Book{}

    rows, err := db.Query("SELECT id, name, author, pages, publication_date FROM books order by id")
    defer rows.Close()
    if err == nil {
        for rows.Next() {
            var id int
            var name string
            var author string
            var pages int
            var publicationDate pq.NullTime

            err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
            if err == nil {
                currentBook := models.Book{ID: id, Name: name, Author: author, Pages: pages}
                if publicationDate.Valid {
                    currentBook.PublicationDate = publicationDate.Time
                }

                books = append(books, currentBook)
            } else {
                return books, err
            }

        }
    } else {
        return books, err
    }
    return books, err
}  

这个html在我的观点 index.html

<table class="table table-striped">
  <thead>
    <tr>
      <th>No</th>
      <th>ID</th>
      <th>Name</th>
      <th>Author</th>
      <th>Pages</th>
      <th>Publication Date</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>

    {{range .books}}
    <tr>
      <td>{{.Nomor}}</td>
      <td>{{.ID}}</td>
      <td>{{.Name}}</td>
      <td>{{.Author}}</td>
      <td>{{.Pages}}</td>
      <td>{{.PublicationDateStr}}</td>
    </tr>
    {{end}}
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="book.html" class="btn btn-primary">New book</a></td>
    </tr>
  </tbody>
</table>  

希望这样的结果

|No.|ID |Name |Author | Pages | Publication Date  |
|1  |2  |Jack |Holly  | 255   | 2017-10-15        |
|2  |4  |hans |Holly  | 255   | 2017-10-15        |
|3  |6  |Juri |Holly  | 255   | 2017-10-15        |
|4  |7  |Hank |Holly  | 255   | 2017-10-15        |

但我得到的是

|No.|ID |Name |Author | Pages | Publication Date | 
|0  |2  |Jack |Holly  | 255   | 2017-10-15       | 
|0  |4  |hans |Holly  | 255   | 2017-10-15       | 
|0  |6  |Juri |Holly  | 255   | 2017-10-15       | 
|0  |7  |Hank |Holly  | 255   | 2017-10-15       |

2 个答案:

答案 0 :(得分:0)

我没有看到Nomor在任何地方填充的属性。

你应该保留一个计数器并执行类似

的操作
setEnabled()

答案 1 :(得分:0)

您可以尝试将切片索引的值分配给变量并尝试打印它,例如

{{range $index, _ := .books}}

并使用索引代替Nomor