当我尝试调试像这样的简单 Golang 程序时,它有效
package main
import "fmt"
func main(){
var name string
name = "Adan"
fmt.Println(name)
}
但是在我的 Go api 中,断点没有命中
看,伙计们,这是我的主要功能,我在其中配置我的服务器
package main
import (
"log"
"os"
...
)
var (
menuRepo psqlrepo.MenuRepository = psqlrepo.NewMenuRepository()
menuService service.MenuService = service.NewMenuService(menuRepo)
menuController controller.MenuController = controller.NewMenuController(menuService)
dishRepo psqlrepo.DishRepo = psqlrepo.NewDishRepo()
dishService service.DishService = service.NewDishService(dishRepo)
dishController controller.DishController = controller.NewDishController(dishService)
httpRouter router.Router = router.NewMuxRouter()
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
httpRouter.POST("/api/menu/{id}", menuController.Create, middleware.ValidateToken)
httpRouter.POST("/api/dish/{menu_id}", dishController.CreateDish, middleware.ValidateToken)
httpRouter.SERVE(os.Getenv("PORT"))
}
这是我设置断点的存储库,我在函数 insertOptionAvailable 的第一个 if 语句中有断点
package psqlrepo
func insertOptionAvailable(optionAvailable entity.OptionsAvailables, optionID int, _db *sqlx.DB) error {
var (
itExist bool
)
if err := _db.QueryRow("select exists (select 1 from optionsAvailabe where name = $1)", optionAvailable.Name).Scan(&itExist); err != nil {
return err
}
if itExist {
return errors.New("this item already exists")
}
optionAvailable.CreatedAt = time.Now()
optionAvailable.UpdatedAt = time.Now()
_, err := _db.Exec("insert into optionsAvailabe(name, quantity, extra_price, option_id, updated_at, created_at) values($1, $2, $3, $4, $5, $6)", optionAvailable.Name, optionAvailable.Quantity, optionAvailable.ExtraPrice, optionID, optionAvailable.UpdatedAt, optionAvailable.CreatedAt)
if err != nil {
return err
}
return nil
}
func (*dishRepo) NewDish(dish entity.Dish, menuId int) (sql.Result, error) {
var (
_db = db.NewPostgreSQLConection()
itExist bool
dishID int
extraItems []entity.ExtraItem
)
defer _db.Close()
if err := _db.QueryRow("select exists (select 1 from dishes where name=$1)", &dish.Name).Scan(&itExist); err != nil {
return nil, err
}
if itExist {
return nil, errors.New("dish already exists")
}
dish.CreatedAt = time.Now()
dish.UpdatedAt = time.Now()
err := _db.QueryRow("insert into dishes(name, description, tags, menu_id, price, created_at, updated_at) values($1, $2, $3, $4, $5, $6, $7) returning id", dish.Name, dish.Description, dish.Tags, menuId, dish.Price, dish.CreatedAt, dish.UpdatedAt).Scan(&dishID)
if err != nil {
return nil, err
}
if len(*dish.Ingredients) > 0 {
for _, ingredient := range *dish.Ingredients {
if err := insertIngredient(ingredient, dishID, _db); err != nil {
return nil, err
}
}
}
if len(*dish.Extras) > 0 {
for _, option := range *dish.Extras {
optionID, err := insertOption(option, dishID, _db)
if err != nil || optionID == -1 {
return nil, err
}
extraItems = append(extraItems, option.ExtraItems...)
if len(extraItems) > 0 {
for _, _optionAvailable := range extraItems {
if err = insertOptionAvailable(_optionAvailable, optionID, _db); err != nil {
return nil, err
}
}
}
}
}
return nil, nil
}
这是我调用 repo 的服务
package service
import (
"database/sql"
...
)
type DishService interface {
NewDish(dish entity.Dish, menuId int) (sql.Result, error)
}
var (
_dishRepo psqlrepo.DishRepo
)
type dishService struct{}
func NewDishService(dishRepo psqlrepo.DishRepo) DishService {
_dishRepo = dishRepo
return &dishService{}
}
func (*dishService) NewDish(dish entity.Dish, menuId int) (sql.Result, error) {
return _dishRepo.NewDish(dish, menuId)
}
这里是控制器
package controller
import (
"encoding/json"
"net/http"
"strconv"
...
)
type DishController interface {
CreateDish(rw http.ResponseWriter, r *http.Request)
}
type dishController struct{}
var (
_dishService service.DishService
)
func NewDishController(dishService service.DishService) DishController {
_dishService = dishService
return &dishController{}
}
func (*dishController) CreateDish(rw http.ResponseWriter, r *http.Request) {
r.Header.Set("Content Type", "json/application")
var (
dish entity.Dish
response entity.Reponse
vars = mux.Vars(r)
)
menuID := vars["menu_id"]
if menuID == "" {
http.Error(rw, "menu id required", http.StatusBadRequest)
return
}
id, err := strconv.Atoi(menuID)
if err != nil {
http.Error(rw, "error with menu id", http.StatusBadRequest)
return
}
if err := json.NewDecoder(r.Body).Decode(&dish); err != nil {
http.Error(rw, "Error getting body", http.StatusBadRequest)
return
}
result, err := _dishService.NewDish(dish, id)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
response = entity.Reponse{
Message: "Dish created successfully",
Error: "",
Data: result,
}
rw.WriteHeader(http.StatusOK)
json.NewEncoder(rw).Encode(&response)
}
在邮递员中,当我将 post 请求发送到 http://localhost:8080/api/dish/13 时,程序运行良好,但断点没有命中。
我尝试在午餐.json 文件上设置几个配置,此时我有这个,但它们都不适合我:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}"
},
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
},
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
},
]
}
我还确保我是否为带有苹果芯片的Mac安装了go和vs-code,我的是m2 MacBook Air,所以我安装了正确的版本。
我在 mac 上使用 VSCode 和 go debugger 时遇到问题,如果项目目录是由 ´ln –s 创建的链接…ˋ 在原始目录中启动 VsCode 有效