0

我有一个网站,有几个不同的功能。我可以使用 localhost:5000/ 访问我的网站,当我使用 vscode-go 调试器在调试模式下运行它时,我收到以下消息 API 服务器正在侦听:127.0.0.1:52238

我有一个返回多个字符串的名称函数,但在调试模式下我无法命中断点。我在我的 Name 函数中放置了一个断点并将 url 放置如下: 127.0.0.1:52238/name但是它没有达到断点。这里会发生什么?如果我正常运行应用程序并输入http://localhost:5000/name然后一切正常,但在调试模式下,我的代码如下127.0.0.1:52238/name不会命中断点或页面。我使用 Go 作为后端 api,所以我需要点击 url 端点来查看发生了什么。有没有办法让我也可以制作调试端口:5000

  -- Main 
 package main

 import (
     "github.com/gorilla/mux"
     "runtime"
     "./Models"
     "./Controllers"
 )

 func main() {

  Controllers.CircleRoutes(r)

     srv := &http.Server{
         ReadTimeout:  20 * time.Second,
         WriteTimeout: 20 * time.Second,
         IdleTimeout:  120 * time.Second,
         Addr:         ":5000",
     }

     srv.ListenAndServe()
    }



   // Circles Route

 package Controllers

func Name(w http.ResponseWriter, r *http.Request) {

    var result string
    r.ParseForm()
    result = "Success"

    io.WriteString(w, result)
}

 func CircleRoutes(r *mux.Router) {
     r.HandleFunc("/name", Name)
 }
4

1 回答 1

1

看起来您正在使用 vscode-go 调试器。您可以从 vscode 的 launch.json 配置文件配置端口。

The config should look like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {
                "GOPATH": <your GOPATH>
            },
            "args": [],
            "showLog": true
        }
    ],
    "go.lintTool": "gometalinter"
}

You can change port from above settings. To find launch.json, just ctrl+P and type launch.json it will show dropdown result of search in your vscode.

于 2018-05-22T09:09:39.893 回答