我从mux.vars[]获得了一个空 map [].

追踪这个问题后,我的多个文件中似乎都有一个空的map[].

而在同一个文件中:我已经将另一个文件作为模块(我相信)导入了import

package main

import(
"github.com/Ny0ttt/go-bookstore/pkg/controllers"
//note that this is imported from powershell, go mod init "github.com/Ny0ttt/go-bookstore"
)

而在运行go run main.go之后,我获得的map[]为map[ID]

在不同的文件中时:

和前一个一样,我收到了一个空的map[]

这是我的文件 struct

Go-Test/cmd/main/main.exe
Go-Test/cmd/main/main.go
Go-Test/pkg/config/app.go
Go-Test/pkg/models/models.go
Go-Test/pkg/controllers/book-controller.go
Go-Test/pkg/utils/utils.go
Go-Test/go.sum
Go-Test/go.mod

我的mod文件包含

module github.com/Ny0ttt/go-bookstore

go 1.20

require (
    github.com/go-sql-driver/mysql v1.5.0
    github.com/gorilla/mux v1.8.0
    github.com/jinzhu/gorm v1.9.16
)

require github.com/jinzhu/inflection v1.0.0 // indirect


以下是我的代码:

在我的控制器中

func GetBookById(w http.ResponseWriter, r *http.Request){
    vars := mux.Vars(r)
    bookId := vars["ID"]
    ID, err := strconv.ParseInt(bookId, 0, 0)
    if err != nil{
        fmt.Println("error while parsing")
        fmt.Println(r)
        fmt.Println(bookId)
        fmt.Println(vars)
    }
    bookDetails, _ := models.GetBookById(ID) //mthis model returns 2 variable but we will be using only one of them. refer models
    res, _ := json.Marshal(bookDetails)
    w.Header().Set("Content-Type","pkglication/json")
    w.WriteHeader(http.StatusOK)
    w.Write(res)
}

在我的路由上

func main() {
    r := mux.NewRouter()
    
    //routes.RegisterBookStoreRoutes(r)
    r.HandleFunc("/book/", controllers.GetBook).Methods("GET")
    r.HandleFunc("/book/", controllers.CreateBook).Methods("POST")
    r.HandleFunc("/book/{ID}", controllers.GetBookById).Methods("GET")
    r.HandleFunc("/book/{bookId}", controllers.UpdateBook).Methods("PUT")
    r.HandleFunc("/book/{bookId}", controllers.DeleteBook).Methods("DELETE")
        http.Handle("/", r)
    log.Fatal(http.ListenAndServe(":2222", r)) // creating a server
}

输出

error while parsing
&{GET /book/1 HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip, deflate, br] Connection:[keep-alive] Postman-Token:[a28eebe1-b6d4-4aff-89ed-d3b93a4ed1df] User-Agent:[PostmanRuntime/7.32.3]] {} <nil> 0 [] false :2222 map[] map[] <nil> map[] 127.0.0.1:52035 /book/1 <nil> <nil> <nil> 0xc000282a20}

map[]

注意:我把这些值打印出来,这样我就可以追踪哪里出了问题,*http.Request没有映射任何值吗?我不确定返回的http[]应该是什么样子的map[].

我寻找了解决方案,但似乎需要对这些替代方案做出更多解释.我已经查到了r.URL.Query,但他们面临的问题是使用不同的路由 struct .

我也在寻找另一种‘mux.RouteMatch’的替代品,但我想我需要一点解释.

推荐答案

问题已解决

我只需将我的项目移动到桌面上(我相信您可以移动到您想要的任何位置,只要您不需要管理员权限来访问目录),它就起作用了.

从现在开始,我将继续在可访问的目录中探索项目.谢谢.

Go相关问答推荐

有没有更简单的方法在Go中编写这个逻辑?

我不能让GIO画一个按钮

如何修复Go中调用GetRawInputDeviceInfA Windows API函数时的错误?

CGO Linux到Windows交叉编译中的未知类型名称

使用!NOT运算符的Golang文本/模板多个条件

如何从 nil 指针创建值

在 go 中,将接收器 struct 从值更改为指针是否向后兼容?

Gorm 在自定义字符串类型上返回 scanner 错误

我突然无法再将我的 GoLang 应用程序部署到 Google AppEngine

如何在 Golang http.Request 对象中读取自定义 ajaxParams

go version -m 输出中的箭头符号=>是什么意思?

仅在工作日运行 cron

将shell输出绑定到Go中的 struct 的最佳方法?

io.Pipe 使用困难

在删除级联时无法在 Gorm 中按预期工作

Golang ACMEv2 HTTP-01 挑战不挑战服务器

每 N 秒运行一次函数,上下文超时

行之间的模板交替设计

如何使用通用字段初始化匿名struct数组

Golang 中的无实体函数