2017-10-16 10 views
0

テーブルの行数を増やすことはできません。
revelフレームワークを使用して、単純なcrudを試してみます。Go langのインクリメント番号

質問は次のとおりです:「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  | 
+1

これはどうやって見えますか? –

+0

'row_number()を使って(order by id)'を使って数字を生成することができます。 –

+0

@EugeneLisitsky質問が更新されました。 –

答えて

0

Iでありますしない Nomorプロパティはどこにでも配置されています。

あなたはカウンターを維持し、あなたが変数にスライスインデックスの値をassinging試してみて、それを印刷してみてください、

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

のようなものとインデックスを使用することができ

... 
currentBook := models.Book{Nomor: count, ID: id, Name: name, Author: author, Pages: pages} 
count = count + 1 
... 
0

ような何かを行う必要がありますNomorの代わりに

関連する問題