beego
go get github.com/astaxie/beego
安裝bee工具
go get github.com/beego/bee
建立新專案
bee new test1
執行專案
cd test1
bee run
網頁顯示也可以直接用html副檔名
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
c.TplName = "index.html"
}
強迫停止服務
ps aux | grep bee | awk '{print $2}' | xargs kill -9
2017年11月16日 星期四
2017年11月15日 星期三
使用revel框架
https://revel.github.io/
注意自己的go路徑
例如
export GOPATH=$HOME/ex/p1
若有下載範例程式,將程式傳到
/home/yau/ex/p1/src
下載相關套件
go get github.com/revel/revel
go get -u -v github.com/revel/cmd/revel
建立專案
revel new myapp
執行
revel run myapp
環境檔conf
app.conf
注意自己的go路徑
例如
export GOPATH=$HOME/ex/p1
若有下載範例程式,將程式傳到
/home/yau/ex/p1/src
下載相關套件
go get github.com/revel/revel
go get -u -v github.com/revel/cmd/revel
建立專案
revel new myapp
執行
revel run myapp
環境檔conf
app.conf
埠號
http.port = 9000
http.port = 9000
使用iris框架
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
2017年11月14日 星期二
debian上安裝golang
sudo apt-get update
抓go語言1.9.2
wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
解開
tar -xvf go1.9.2.linux-amd64.tar.gz
移動目錄
sudo mv go /usr/local
vi ~/.profile
export GOROOT=/usr/local/go
export GOPATH=$HOME/ex/p1
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
立即生效
source ~/.profile
顯示版本
$ go version
go version go1.9.2 linux/amd64
顯示環境
$ go env
簡易練習
mkdir ex
cd ex
vi web.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "GO語言, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
編譯(注意web.go要utf-8編碼才能顯示中文)
go build web.go
編譯好會產生一個執行檔
執行
./web
192.168.0.5:8080 就會看到網頁
問題:關閉終端機,服務就消失
解決1:裝tmux
sudo apt-get install tmux
關閉終端機服務還能繼續跑
tmux new -s 名稱
例如 tmux new -s my8080
這樣才知道自己的服務
分割 / 切割視窗 (切出來的叫pane)
Ctrl-b q Show pane numbers
Ctrl-b alt按著,再按上下左右, 可以調整視窗大小
Ctrl-b % 直切
Ctrl-b :"split-window" 橫切
Ctrl-b Ctrl-o 調換 切割視窗 的 位置
Ctrl-b o Switch to the next pane
Ctrl-b 上下左右 跳到上下左右的分割視窗
Ctrl-b ! 將目前的pane抽成window
Ctrl-b x 移除目前的pane
Ctrl-b + c:建立新的視窗;
Ctrl-b + d:卸載目前的 client;
Ctrl-b + l:與先前選擇的視窗間切換;
Ctrl-b + n:移動到下個視窗;
Ctrl-b + p:移動到上個視窗;
Ctrl-b + &:刪除目前的視窗;
Ctrl-b + ,:重新命名目前的視窗;
Ctrl-b + %:將目前的視窗分離到兩個區塊;
Ctrl-b + q:顯示各分割區塊的號碼(用來切換到不同的區塊)
Ctrl-b + o:切換到下個區塊;
Ctrl-b + ?:列出所有快捷鍵的說明;
Ctrl-b + w:列出目前 clinet 的視窗,可以用數字鍵切換;
按住ctrl 和b 再按d離開
顯示服務
tmux ls
關閉服務
tmux attach -t 編號
解決2:採用linux supervisor
抓go語言1.9.2
wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
解開
tar -xvf go1.9.2.linux-amd64.tar.gz
移動目錄
sudo mv go /usr/local
vi ~/.profile
export GOROOT=/usr/local/go
export GOPATH=$HOME/ex/p1
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
立即生效
source ~/.profile
顯示版本
$ go version
go version go1.9.2 linux/amd64
顯示環境
$ go env
簡易練習
mkdir ex
cd ex
vi web.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "GO語言, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
編譯(注意web.go要utf-8編碼才能顯示中文)
go build web.go
編譯好會產生一個執行檔
執行
./web
192.168.0.5:8080 就會看到網頁
問題:關閉終端機,服務就消失
解決1:裝tmux
sudo apt-get install tmux
關閉終端機服務還能繼續跑
tmux new -s 名稱
例如 tmux new -s my8080
這樣才知道自己的服務
分割 / 切割視窗 (切出來的叫pane)
Ctrl-b q Show pane numbers
Ctrl-b alt按著,再按上下左右, 可以調整視窗大小
Ctrl-b % 直切
Ctrl-b :"split-window" 橫切
Ctrl-b Ctrl-o 調換 切割視窗 的 位置
Ctrl-b o Switch to the next pane
Ctrl-b 上下左右 跳到上下左右的分割視窗
Ctrl-b ! 將目前的pane抽成window
Ctrl-b x 移除目前的pane
Ctrl-b + c:建立新的視窗;
Ctrl-b + d:卸載目前的 client;
Ctrl-b + l:與先前選擇的視窗間切換;
Ctrl-b + n:移動到下個視窗;
Ctrl-b + p:移動到上個視窗;
Ctrl-b + &:刪除目前的視窗;
Ctrl-b + ,:重新命名目前的視窗;
Ctrl-b + %:將目前的視窗分離到兩個區塊;
Ctrl-b + q:顯示各分割區塊的號碼(用來切換到不同的區塊)
Ctrl-b + o:切換到下個區塊;
Ctrl-b + ?:列出所有快捷鍵的說明;
Ctrl-b + w:列出目前 clinet 的視窗,可以用數字鍵切換;
按住ctrl 和b 再按d離開
顯示服務
tmux ls
關閉服務
tmux attach -t 編號
解決2:採用linux supervisor
訂閱:
文章 (Atom)