首頁  >  文章  >  後端開發  >  分享5種檔案變更時自動重載Go程式的方法

分享5種檔案變更時自動重載Go程式的方法

藏色散人
藏色散人轉載
2020-11-03 15:19:582801瀏覽

許多人希望在寫GO時擁有即時載入程式碼(熱編譯)的效果,特別是那些習慣使用JavaScript,Python和Ruby等解釋語言的人,本文介紹了5種即時重新載入分享5種檔案變更時自動重載Go程式的方法程式的方法。

本文假設已安裝分享5種檔案變更時自動重載Go程式的方法編譯器,並且已將分享5種檔案變更時自動重載Go程式的方法GOPATH/bin

路徑新增至PATH環境變數。

在開始之前,我們先建立一個簡單的web伺服器,可以回傳回應內容」Hello,World」。

package mainimport (
    "net/http")func main() {
    http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, World"))
    })

    http.ListenAndServe(":5000", nil)}
Method 1: 使用Air

Air 是命令列程序,可以為分享5種檔案變更時自動重載Go程式的方法程式提供即時程式碼載入。

透過執行以下指令來安裝Air。
go get -u github.com/cosmtrek/air
下一步,在使用專案的根目錄中建立Air設定檔

.air.conf

# .air.conf
# toml配置文件来源于 [Air](https://github.com/cosmtrek/air)# 工作区间
# .(当前目录)或绝对路径, 注意这些目录都在根目录下面.root = "." tmp_dir = "tmp"[build]# 只是普通的shell命令。 可以使用`make`。
cmd = "go build -o ./tmp/main ."# `cmd`配置命令输出的二进制文件的位置。
bin = "tmp/main"# 自定义二进制输出。
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"# 监听的文件扩展后缀列表。
include_ext = ["go", "tpl", "tmpl", "html"]# 忽略这些文件扩展名或目录。
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]# 如果指定,则监听包含这些文件。
include_dir = []# 忽略文件列表.exclude_file = []# 如果文件修改太频繁,则不必在每次修改时都立刻触发构建,设置触发等待时间。
delay = 1000 # ms
# 发生编译错误时,是否停止旧的二进制程序。
stop_on_error = true# 该日志文件放置在tmp_dir中。
log = "air_errors.log"[log]# 日志是否显示时间
time = false[color]# 自定义每类输出的颜色。 如果找不到这个颜色,使用原本的日志输出演示。
main = "magenta"watcher = "cyan"build = "yellow"runner = "green"[misc]# 退出时是否删除临时目录
clean_on_exit = true

設定是簡單明了的,請根據你的專案狀況去調整。

最後,不要使用常用的go run指令來執行分享5種檔案變更時自動重載Go程式的方法程序,而應使用

air

指令來啟動程式。

Method 2: docker執行Ari這個方法需要使用docker,如果你沒有安裝,可以依照

我們仍將使用Air函式庫,因此仍然需要Air設定檔。如果你還沒有,請建立一個設定檔。 Docker映像

cosmtrek/air

附帶安裝了Air指令,且GOPATH環境變數設定為

/go

我們只需要將我們的專案目錄掛載到Docker容器的GOPATH中,並使用-p暴露需要使用的連接埠。我們可以透過執行docker run命令來實現這一點:<pre class="brush:php;toolbar:false">docker run -it --rm -w &lt;working_dir&gt; -v &lt;project_folder&gt;:&lt;mount_point&gt; -p &lt;host_port&gt;:&lt;container_port&gt; &lt;image_name&gt;&lt;/image_name&gt;&lt;/container_port&gt;&lt;/host_port&gt;&lt;/mount_point&gt;&lt;/project_folder&gt;&lt;/working_dir&gt;</pre>就我而言,我需要執行以下命令:<pre class="brush:php;toolbar:false">docker run -it --rm -w /go/src/github.com/praveen001/live-reloading -v /go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading -p 5000:5000 cosmtrek/air</pre>

解釋:

# #使用-v參數將專案目錄

/home/praveen/go/src/github.com/praveen001/live-reloading

掛載到容器裡面的GOPATH中的目錄

/go/src/ github.com/praveen001/live-reloading

-v /home/praveen/go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading
使用

-w參數指定掛載目錄成工作目錄。

-w /go/src/github.com/praveen001/live-reloading
Web伺服器正在監聽連接埠5000,因此需要使用-p標誌將容器連接埠5000暴露到主機連接埠5000。

-p 5000:5000
最後,指定docker映像名稱cosmtrek / air。

Method 3: 使用Gin

Gin是另一個用於即時重新載入分享5種檔案變更時自動重載Go程式的方法應用程式的命令列程式。

透過執行以下命令來安裝Gin。

go get github.com/codegangsta/gin
而不是使用通常的go run main.go

命令運行應用程序,而是使用

gin

命令。

就我而言,

--appPort參數告訴Gin監聽埠5000,--port參數告訴Gin代理監聽埠3000埠

gin --appPort 5000 --port 3000
現在使用位址

http://localhost:3000存取Gin程式.如果要排除監聽那個目錄可以使用--excludeDir

參數,例如:

gin --appPort 5000 --port 3000 --excludeDir ./frontend
如果你項目使用Gin實作載入沒有啟動埠監聽的程序,你們必須使用--immediate參數。但是Gin仍然會去5000埠。

你可以在這找到所有受支援的參數Gin的Github.

#Method 4: 使用Nodemon

Nodemon是另一個用於實時重新載入Node應用程式的命令列程式。但是可以透過使用

--exec

參數設定啟動命令用於啟動其他應用程式。

Nodemon需要安裝Nodejs和NPM。如果沒有安裝,可以按照nodejs的官方文件進行安裝.

運行以下命令來安裝nodemon:

npm install -g nodemon
現在,我們可以透過執行以下命令來使用Nodemon運行Web伺服器:
nodemon --exec go run main.go --signal SIGTERM
如果要設定Nodemon,請在專案的根目錄中建立設定檔

nodemon.json

。完整可用的範例設定檔

Method 5: 使用Fresh

#Fresh 是另一個GO實現的用於即時重新載入分享5種檔案變更時自動重載Go程式的方法的程式

安裝Fresh

go get github.com/pilu/fresh
而不是使用常用的go run main.go

命令來運行應用程序,而是使用

fresh

命令。

fresh
要設定Fresh,需要在專案的根目錄中建立一個設定檔

runner.conf這是一個範例設定檔。

root:              .tmp_path:          ./tmp
build_name:        runner-build
build_log:         runner-build-errors.log
valid_ext:         .go, .tpl, .tmpl, .html
no_rebuild_ext:    .tpl, .tmpl, .html
ignored:           assets, tmp
build_delay:       600colors:            1log_color_main:    cyan
log_color_build:   yellow
log_color_runner:  green
log_color_watcher: magenta
log_color_app:

總結

還有許多其他工具,例如:

  • Facebook's Watchman
  • Realize
  • Reflex
  • Even a custom–built solution

原文網址:https ://techinscribed.com/5-ways-to-live-reloading-go-applications/

翻譯網址:https://learnku.com/go/t/51189

以上是分享5種檔案變更時自動重載Go程式的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除