Golang development: environment (5) use of real-time loading tool gin

The gin tool is a very useful and effective tool in golang development, which effectively improves the efficiency of developing and debugging go programs.

Compile gin in real time

Why use gin

We know that golang is a compiled language, which means that every time the go program is changed, if you need to view the result of the change, you must recompile it, that is, go build. Like we are engaged in the development of go web, it may be from other interpreted languages. Yes, it is particularly not suitable for this kind of debugging and development. After changing the code, you need to compile go build. Then, gin appeared to solve this demand.

Look at the official explanation of gin

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

Install gin

Of course, the first is of course 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]

The above message indicates that the installation is successful.

Use gin

Understand the most commonly used gin commands in development

--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 following system variable names to set these variables. These
commands will have no problem if you master the basic development.

Give a chestnut

Create a new web service
Look at the simple web service code in 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 this code is compiled, it will listen to port 9090 after starting the WEB service.
We use gin to compile and start this service.
My physical machine to virtual machine is mapped

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 in to the virtual machine to start the 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 be added later, path and build are in the current directory, so use the default.
Let's test under curl

curl http://192.168.0.10:3000
Hello world!

We modify the output file

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

When Ctrl+S saves, see the compiled information

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

Let's test again

curl http://192.168.0.10:3000
Hello China!

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

The result is the same as the above command line.

end

With Gin, go web debugging is basically the same as PHP NODE and other interpreted languages. You don't need to request a test after go build every time. You only need to start the shell script, and gin will automatically compile it when you change the code.

Want to get more, follow the official instructions of gin
https://github.com/codegangsta/gin

Guess you like

Origin blog.csdn.net/feifeixiang2835/article/details/92848583