golang Development :( V) load in real time using the tools of gin

gin is golang development tool very useful and effective tool, effectively improve the efficiency of program development and debugging go.

Why use gin

We know golang is a compiled language, which means each modification programs go, if you need to see the results of changes have to be recompiled once, go build. Like we do go web development, possibly across from the other interpreted languages , it is particularly suited to this kind of debugging and development, change the code to compile complete go build. Then, gin appeared just to address this need.

Look official explanation of gin

gin is a simple command-line utility for real-time reload Go Web applications. Just run the gin in your application directory, your web application will be gin as a proxy service. When the gin detects a code change, it will automatically recompile the code. Your application will receive an HTTP request to restart the next time.

Niubi Niubi not say

Installation gin

Of course, the first of course is to install gin in our virtual machine

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]

Appears above message indicates that the installation was successful.

Use gin

Learn the most common used in the development of several gin command

--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]

You can use the system variable names to follow these variable settings
these orders have mastered the basic development on nothing unusual problem.

For chestnuts

Create a new web service
simple web service code look of Go

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)
    }
}

After the code is compiled, after starting 9090 listens WEB service port.
We compiled using gin to start the service
of my physical-to-virtual mapping is

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

We vagrant Log virtual machine starts service

sudo vagrant ssh
cd 项目目录
gin -p 3000 -a 9090 -b test.bin --all run
表示监听虚拟机的3000端口,将请求转发给9000端口,生成的二进制执行文件 test.bin,所有文件的改动都会引起项目编译

Of course, the above parameters can all be added later, path and build are in the current directory, so I use the default.
Our curl test

curl http://192.168.0.10:3000
Hello world!

We modify the output file under

fmt.Fprintf(w, "Hello China!")

Ctrl + S to save time to see a compilation of information

[gin] Building...
[gin] Build finished

We next tested again

curl http://192.168.0.10:3000
Hello China!

Of course the way, we can also use system variables, start gin service
creation 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

With the results of the above command line, like a hair.

end

Once you have Gin, go web debugging basic just like PHP NODE and other interpreted languages, and after do not always go build recurrence request a test, only need to start the shell script, gin automatically help when you change the code to compile.

Official want to connect more attention to the next gin description
https://github.com/codegangsta/gin

Guess you like

Origin www.cnblogs.com/golangcode/p/10963961.html