轉載
Go基礎學習記錄 - 編寫Web應用程序 - 從新調整項目目錄結構(一)css
前面的一些文章介紹,徹底能夠作一個簡單的Web應用,可是若是用過PHP或者是Python等語言的框架開發Web應用的時候會發現,從結構目錄上來講徹底是一個成熟的架構了,可是對於Golang來講,彷佛並無什麼成熟的架構,網上大多數現成的架構,也都是很隨意,好比最流行的Beego,大概看了下,功能很齊全,能夠直接上手使用,可是對於像我習慣MVC方式的,這不得不讓我以爲,這樣的寫代碼方式,彷佛有些讓我不習慣很習慣,下面讓我來踩踩坑,作個適合本身開發的架構。html
首先咱們把前面文章中的視圖作下整理,將view.html和edit.html,轉移到templates文件夾,之後這個文件夾就是咱們本身放模板的文件夾。同時咱們將view.html和edit.html進行下UI,我這邊使用了Bootstrap4.0版本,代碼分別以下jquery
templates/view.htmlbootstrap
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>{{.Title}}</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="col-md-12"> <div class="text-center"> <h1> {{.Title}}</h1> </div> <p>[<a href="/edit/{{.Title}}">edit</a>]</p> <div>{{printf "%s" .Body}}</div> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>
templates/edit.html架構
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Editing {{.Title}}</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="col-md-12"> <div class="text-center"> <h1>Editing:【 {{.Title}}】</h1> </div> <form action="/save/{{.Title}}" method="POST"> <div class="form-group"> <textarea class="form-control" name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>
同時修改templates框架
有原來的學習
var templates = template.Must(template.ParseFiles("template/edit.html", "template/view.html"))
修改成code
var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html"))
從新編譯並運行代碼orm
$ go install $ wiki
完美,一切正常。(正常不表明沒問題),後面繼續cdn