2017-04-07 47 views
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 
+2

有没有办法让一个去程序只是“在后台运行”。你必须自己做。 – JimB

+1

奇怪的是,它的确如此。我认为这是VS Code和DLV中的一个错误。 –

+0

不,我保证它不会,Go程序是多线程的,不能自行分叉。更有可能它立即退出,你检查了从'http.ListenAndServe'返回的错误吗? – JimB

回答

1

您是否尝试过使用nohup运行服务器?

nohup go run server.go &