I get an empty map[] from mux.Vars[].
After tracing the problem, it seems to be an empty map[]
passed through my multiple files.
While in the same file: I have imported the other file as a module(I believe) in the 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"
)
while after running go run main.go
, I obtain the map[] as map[ID]
While in a different file:
as per imported like the previous one, I receive an empty map[]
Here 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
and 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
Here are my codes :
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)
}
In my Routes
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
}
The 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[]
Note: I print out the values so I can trace where it did go wrong, *http.Request
does not map any values? I am not sure what map[] in return http.Request should look like.
I looked up for solutions, but it seems like i need more explaination on those alternatives. I have look up on r.URL.Query
but the problem that they are facing is when using a different route structure.
I have also looked up for another alternative with `mux.RouteMatch` but I think I could use a little bit of explanation.
Problem Solved
I simply moved my project into the desktop (I am sure you may move wherever you want as long as you don't need administrator power to access the directory ) and it works.
I will continue exploring projects in a accessible directory from now on. Thank you.