go get -u github.com/kataras/iris
建立一個專案目錄
建立main.go檔案
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
//
// Need to use a custom regexp instead?
// Easy;
// Just mark the parameter's type to 'string'
// which accepts anything and make use of
// its `regexp` macro function, i.e:
// app.Get("/user/{id:string regexp(^[0-9]+$)}")
app.Get("/user/{id:long}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
app.Run(iris.Addr(":8080"))
}
建立一個次目錄views放網頁hello.html
<!-- file: ./views/hello.html --> <html> <head> <title>你好練習</title> </head> <body> <h1>{{.message}}</h1> </body> </html>
執行程式
go run main.go
安裝rizla這樣網頁改變才不用一直重新啟動服務
go get -u github.com/kataras/rizla
往後啟動程式 輸入 rizla main.go
沒有留言:
張貼留言