go - 如何调试特定功能?
问题描述
我有一个网站,有几个不同的功能。我可以使用 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)
}
解决方案
看起来您正在使用 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.
推荐阅读
- django - 视图 formApp.views.formRegister 没有返回 HttpResponse 对象
- vue.js - Nuxt.config.js noscript innerHtml 在浏览器上呈现
- java - 如何更改 intellij IDEA 上特定代码行的字体?
- python - PermissionError: [WinError 5] 运行脚本时
- networking - SNMP 代理如何知道要读取哪个资源?
- excel - 如何在 Office JS 中使用 Excel.RangeAreas?
- azure-devops - Azure Pipelines - CI/CD:如何针对 DB 执行文件夹中的所有 .sql 文件
- javascript - 如何在 react.js 中获取代码输入作为组件
- python-3.7 - 如果存在,Django OneToOne Reverse Relation 会给出 RelatedObjectDoesNotExist
- html - CSS中的if else语句?