0
go1.8go run app.go始終在後臺運行
下面總是如何在後臺運行?我有一個名爲文件server.go,並做 「go run server.go
」
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there X, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
VS代碼Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
}
]
}
更新:我在VS碼端得到這個錯誤:
2017/04/07 16:41:41 debugger.go:257: created breakpoint: &api.Breakpoint{ID:1, Name:"", Addr:0x12063ef, File:"/Users/X/Documents/X/play/go/server.go", Line:9, FunctionName:"main.handler", Cond:"", Tracepoint:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(nil), LoadLocals:(*api.LoadConfig)(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0}
2017/04/07 16:41:41 debugger.go:412: continuing
有沒有辦法讓一個去程序只是「在後臺運行」。你必須自己做。 – JimB
奇怪的是,它的確如此。我認爲這是VS Code和DLV中的一個錯誤。 –
不,我保證它不會,Go程序是多線程的,不能自行分叉。更有可能它立即退出,你檢查了從'http.ListenAndServe'返回的錯誤嗎? – JimB