Why mux.Vars() returns an empty map

WBOY
Release: 2024-02-09 16:09:09
forward
1212 people have browsed it

为什么 mux.Vars() 返回空映射

php editor Youzi is here to answer a common question: Why is an empty mapping returned when using the mux.Vars() method? This problem may be encountered during the development process, usually due to some configuration or usage problems. In this article, we will discuss the possible causes in detail and provide solutions to help developers quickly solve this problem. Let’s take a look!

Question content

I get an empty map[] from mux.vars[].

After tracking the issue, it appears that an empty map[] is passed through multiple of my files.

In the same file: I have imported another file as a module (I believe) in 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"
)
Copy after login

After running go run main.go, I get the map[] as mapp[id]

In different files:

Following the same import as the previous one, I receive an empty map[]

This is my file structure

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
Copy after login

My mod file contains

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
Copy after login

This is my code:

In my controller

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)
}
Copy after login

In my route

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
}
Copy after login

Output

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[]
Copy after login

NOTE: I print out the values ​​so I can track down what went wrong, *http.request doesn't map any values? I'm not sure what the map[] that returns http.request should look like.

I searched for solutions but it seems I need more explanation of these alternatives. I looked up r.url.query but the problem they face is when using different routing structures.

I'm also looking for another alternative using `mux.routematch`, but I think I could use some explanation.

Solution

Problem solved

I just moved my project to the desktop (I'm sure you can move it anywhere you want, as long as you don't need admin rights to access the directory) and it works.

From now on, I will continue to explore projects in accessible directories. Thanks.

The above is the detailed content of Why mux.Vars() returns an empty map. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!