質問編集履歴

1

文章の修正

2020/10/28 15:25

投稿

nagi
nagi

スコア66

test CHANGED
@@ -1 +1 @@
1
- go言語でtemplateを呼ぶ際のディレクトリ構造
1
+ go言語でviewを呼ぶ際のディレクトリ構造
test CHANGED
@@ -14,206 +14,210 @@
14
14
 
15
15
 
16
16
 
17
+ 一覧表示させるデータはDBから取得出来ている前提です。
18
+
19
+
20
+
17
21
  単純にBookListメソッドを以下のようにパスを指定するだけではだめでした。
18
22
 
19
23
  ```go
20
24
 
21
25
  c.HTML(200, "/book/index.html", gin.H{"books": BookLists})
22
26
 
27
+ ```
28
+
29
+
30
+
31
+
32
+
23
33
  ```go
24
34
 
35
+ package controllers
36
+
37
+
38
+
39
+ import (
40
+
41
+ "errors"
42
+
43
+ "fmt"
44
+
45
+ "gin_sample/model"
46
+
47
+ "gin_sample/service"
48
+
25
- データ一覧表示させるデータはDBから取得出来ている前提です。
49
+ "html/template"
50
+
26
-
51
+ "log"
52
+
27
-
53
+ "net/http"
54
+
55
+ "strconv"
56
+
57
+
58
+
59
+ "github.com/gin-gonic/gin"
60
+
61
+ _ "github.com/go-sql-driver/mysql"
62
+
63
+ "github.com/go-xorm/xorm"
64
+
65
+ )
66
+
67
+
68
+
69
+ // DbEngine DB
70
+
71
+ var DbEngine *xorm.Engine
72
+
73
+ var tpl *template.Template
74
+
75
+
76
+
77
+ //Book 書籍情報一覧
78
+
79
+ type Book struct {
80
+
81
+ ID int `xorm:"pk autoincr int" form:"id" json:"id"`
82
+
83
+ Title string `json:"title"        xorm:"'title'"`
84
+
85
+ Author string `json:"author"        xorm:"'author'"`
86
+
87
+ Content string `json:"content"        xorm:"'content'"`
88
+
89
+ }
90
+
91
+
92
+
93
+ //BookList 全件
94
+
95
+ func BookList(c *gin.Context) {
96
+
97
+ bookService := service.BookService{}
98
+
99
+ BookLists := bookService.GetBookList()
100
+
101
+ c.HTML(200, "index.html", gin.H{"books": BookLists})
102
+
103
+ }
104
+
105
+
106
+
107
+
108
+
109
+ ```
110
+
111
+
112
+
113
+ ```html
114
+
115
+
116
+
117
+ <html>
118
+
119
+ <h1>記事一覧</h1>
120
+
121
+ <a href="new">新規作成</a><br>
122
+
123
+ <table>
124
+
125
+ <tr>
126
+
127
+ <th>ID</th>
128
+
129
+ <th>タイトル</th>
130
+
131
+ <th>著者名</th>
132
+
133
+ <th>内容</th>
134
+
135
+ </tr>
136
+
137
+ {{ range .books }}
138
+
139
+ <form action="/book/edit/{{.ID}}" method="get">
140
+
141
+ <input type="hidden" name="id" value="{{ .ID }}" />
142
+
143
+ <tr>
144
+
145
+ <td>{{ .ID }}</td>
146
+
147
+ <td>{{ .Title }}</td>
148
+
149
+ <td>{{ .Author }}</td>
150
+
151
+ <td>{{ .Content }}</td>
152
+
153
+ <td><input type="submit" value="更新" /></td>
154
+
155
+ <td><a href="/book/delete/{{.ID}}">削除</a></td>
156
+
157
+ </tr>
158
+
159
+ </form>
160
+
161
+ {{end}}
162
+
163
+ </table>
164
+
165
+ </html>
166
+
167
+ ```
28
168
 
29
169
  ```go
30
170
 
31
- package controllers
171
+ package router
32
172
 
33
173
 
34
174
 
35
175
  import (
36
176
 
37
- "errors"
38
-
39
- "fmt"
40
-
41
- "gin_sample/model"
42
-
43
- "gin_sample/service"
177
+ "gin_sample/controllers"
44
-
45
- "html/template"
46
-
47
- "log"
48
178
 
49
179
  "net/http"
50
180
 
51
- "strconv"
52
-
53
-
54
-
55
181
  "github.com/gin-gonic/gin"
56
182
 
57
- _ "github.com/go-sql-driver/mysql"
58
-
59
- "github.com/go-xorm/xorm"
60
-
61
183
  )
62
184
 
63
185
 
64
186
 
65
- // DbEngine DB
66
-
67
- var DbEngine *xorm.Engine
68
-
69
- var tpl *template.Template
70
-
71
-
72
-
73
- //Book 書籍情報一覧
187
+ //GetRouter ルートを定義
188
+
74
-
189
+ func GetRouter() *gin.Engine {
190
+
75
- type Book struct {
191
+ router := gin.Default()
76
-
77
- ID int `xorm:"pk autoincr int" form:"id" json:"id"`
192
+
78
-
79
- Title string `json:"title"        xorm:"'title'"`
80
-
81
- Author string `json:"author"        xorm:"'author'"`
82
-
83
- Content string `json:"content"        xorm:"'content'"`
193
+ router.LoadHTMLGlob("template/*.html")
194
+
195
+ book := router.Group("/book")
196
+
197
+ {
198
+
199
+ book.GET("/list", controllers.BookList)
200
+
201
+ book.GET("/new", func(c *gin.Context) {
202
+
203
+ c.HTML(200, "new.html", gin.H{})
204
+
205
+ })
206
+
207
+ book.POST("/add", controllers.BookAdd)
208
+
209
+ book.GET("/edit/:id", controllers.BookEdit)
210
+
211
+ book.POST("/update/:id", controllers.BookUpdate)
212
+
213
+ book.GET("/delete/:id", controllers.BookDelete)
214
+
215
+ }
216
+
217
+
218
+
219
+ return router
84
220
 
85
221
  }
86
222
 
87
-
88
-
89
- //BookList 全件
90
-
91
- func BookList(c *gin.Context) {
92
-
93
- bookService := service.BookService{}
94
-
95
- BookLists := bookService.GetBookList()
96
-
97
- c.HTML(200, "index.html", gin.H{"books": BookLists})
98
-
99
- }
100
-
101
-
102
-
103
-
104
-
105
- ```
223
+ ```
106
-
107
-
108
-
109
- ```html
110
-
111
-
112
-
113
- <html>
114
-
115
- <h1>記事一覧</h1>
116
-
117
- <a href="new">新規作成</a><br>
118
-
119
- <table>
120
-
121
- <tr>
122
-
123
- <th>ID</th>
124
-
125
- <th>タイトル</th>
126
-
127
- <th>著者名</th>
128
-
129
- <th>内容</th>
130
-
131
- </tr>
132
-
133
- {{ range .books }}
134
-
135
- <form action="/book/edit/{{.ID}}" method="get">
136
-
137
- <input type="hidden" name="id" value="{{ .ID }}" />
138
-
139
- <tr>
140
-
141
- <td>{{ .ID }}</td>
142
-
143
- <td>{{ .Title }}</td>
144
-
145
- <td>{{ .Author }}</td>
146
-
147
- <td>{{ .Content }}</td>
148
-
149
- <td><input type="submit" value="更新" /></td>
150
-
151
- <td><a href="/book/delete/{{.ID}}">削除</a></td>
152
-
153
- </tr>
154
-
155
- </form>
156
-
157
- {{end}}
158
-
159
- </table>
160
-
161
- </html>
162
-
163
- ```
164
-
165
- ```go
166
-
167
- package router
168
-
169
-
170
-
171
- import (
172
-
173
- "gin_sample/controllers"
174
-
175
- "net/http"
176
-
177
- "github.com/gin-gonic/gin"
178
-
179
- )
180
-
181
-
182
-
183
- //GetRouter ルートを定義
184
-
185
- func GetRouter() *gin.Engine {
186
-
187
- router := gin.Default()
188
-
189
- router.LoadHTMLGlob("template/*.html")
190
-
191
- book := router.Group("/book")
192
-
193
- {
194
-
195
- book.GET("/list", controllers.BookList)
196
-
197
- book.GET("/new", func(c *gin.Context) {
198
-
199
- c.HTML(200, "new.html", gin.H{})
200
-
201
- })
202
-
203
- book.POST("/add", controllers.BookAdd)
204
-
205
- book.GET("/edit/:id", controllers.BookEdit)
206
-
207
- book.POST("/update/:id", controllers.BookUpdate)
208
-
209
- book.GET("/delete/:id", controllers.BookDelete)
210
-
211
- }
212
-
213
-
214
-
215
- return router
216
-
217
- }
218
-
219
- ```