gin 工具是golang開發中很是有用且有效的工具,有效的提升了開發調試go程序的效率。git
咱們知道golang是編譯型語言,這就表示go程序的每次改動,若是須要查看改動結果都必須從新編譯一次,即go build .像咱們從事go web的開發,多是從其餘解釋型語言跨過來的,就特別的不適應這種調試開發,改完代碼須要編譯go build。而後,gin的出現就爲了解決這種需求。github
看下gin 的官方解釋golang
gin是一個簡單的命令行實用程序,用於實時從新加載Go Web應用程序。 只需在您的應用程序目錄中運行gin ,您的網絡應用程序將以 gin 做爲代理服務。 當gin檢測到有代碼更改時,它會自動從新編譯代碼。 您的應用將在下次收到HTTP請求時從新啓動。web
固然,第一是固然是在咱們的虛擬機中安裝 ginshell
vagrant ssh go get github.com/codegangsta/gin gin -h NAME: gin - A live reload utility for Go web applications. USAGE: gin [global options] command [command options] [arguments...] VERSION: 0.0.0 COMMANDS: run, r Run the gin proxy in the current working directory env, e Display environment variables set by the .env file help, h Shows a list of commands or help for one command GLOBAL OPTIONS: --laddr value, -l value listening address for the proxy server [$GIN_LADDR] --port value, -p value port for the proxy server (default: 3000) [$GIN_PORT] --appPort value, -a value port for the Go web server (default: 3001) [$BIN_APP_PORT] --bin value, -b value name of generated binary file (default: "gin-bin") [$GIN_BIN]
出現上面的提示信息表示安裝成功了。服務器
瞭解日常開發中使用最多的幾個gin的命令網絡
--laddr value, -l value listening address for the proxy server [$GIN_LADDR] 監聽代理服務器的地址 系統變量[$GIN_LADDR] --port value, -p value port for the proxy server (default: 3000) [$GIN_PORT] 代理服務器的端口號 默認3000 系統變量[$GIN_PORT] --appPort value, -a value port for the Go web server (default: 3001) [$BIN_APP_PORT] 轉發給Go web服務的端口 默認3001 系統變量[$BIN_APP_PORT] --bin value, -b value name of generated binary file (default: "gin-bin") [$GIN_BIN] Go生成的二進制可執行文件的名稱 默認gin-bin 系統變量[$GIN_BIN] --path value, -t value Path to watch files from (default: ".") [$GIN_PATH] 監聽文件改動的目錄 默認 . 系統變量[$GIN_PATH] --build value, -d value Path to build files from (defaults to same value as --path) [$GIN_BUILD] 編譯Go 程序的目錄 默認 . 系統變量[$GIN_BUILD] --all reloads whenever any file changes, as opposed to reloading only on .go file change 系統變量[$GIN_ALL] 監聽全部文件的修改,都會從新編譯。若是不加all就只會監聽go文件的修改 系統變量[$GIN_ALL]
可使用後面的系統變量名進行這些變量設置
這幾個命令掌握了基本日常開發就沒啥問題了。app
新建一個web服務
看下Go的簡單的web服務代碼ssh
package main import ( "fmt" "net/http" "log" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world!") } func main() { http.HandleFunc("/", sayhelloName) err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
這段代碼編譯完成後,啓動WEB服務後會監聽9090端口。
咱們使用 gin 來編譯啓動這個服務
個人物理機到虛擬機映射的是curl
192.168.0.10 配置 Vagrant.configure("2") do |config| config.vm.box = "base" config.vm.box_check_update = false config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "private_network", ip: "192.168.0.10" config.vm.synced_folder "/data/www","/data/www",create:true config.ssh.username = "root" config.ssh.private_key_path = "/Users/XXX/.ssh/id_rsa" end
咱們vagrant 登陸虛擬機啓動服務
sudo vagrant ssh cd 項目目錄 gin -p 3000 -a 9090 -b test.bin --all run 表示監聽虛擬機的3000端口,將請求轉發給9000端口,生成的二進制執行文件 test.bin,全部文件的改動都會引發項目編譯
固然了上面的參數都是能夠在後面添加的,path和build都在當前目錄下,因此就使用默認的 .
咱們curl測試下
curl http://192.168.0.10:3000 Hello world!
咱們修改下輸出文件
fmt.Fprintf(w, "Hello China!")
Ctrl+S保存的時候看到有編譯的信息
[gin] Building... [gin] Build finished
咱們再次測試下
curl http://192.168.0.10:3000 Hello China!
固然了,咱們也可使用系統變量的方式,啓動 gin服務
建立test.sh
export GIN_PORT="3000" export BIN_APP_PORT="9090" export GIN_BIN="test.bin" export GIN_ALL=1 gin run chmod +x test.sh ./test.sh
跟上面的命令行的結果一毛同樣。
有了Gin以後,go web調試基本就跟PHP NODE等的解釋型語言同樣了,不用每次都go build以後再發請求測試,只須要啓動 shell腳本,gin自動幫你在改動代碼的時候編譯。
想要了接更多,關注下 gin的官方 說明
https://github.com/codegangsta/gin