我创造了路由有组织的方式.但它似乎没有到达我的controller文件夹.我没有从控制器文件夹中得到响应.有人能告诉我这里犯了什么错误或错误吗?谢谢

这是主文件

https://go.dev/play/p/vSVk_78pcF4

在我的浏览器上,我正在走路由

localhost:8000/api/v1//tasks

http://localhost:8000/api/v1/tasks

但这两条路都告诉no page found

完整代码:

package main

import (
    "fmt"
    "net/http"

    "github.com/sayedulkrm/go-curd-todo/routes"
)

func main() {

    taskRouter := routes.TaskRoutes()

    mainRouter := http.NewServeMux()

    mainRouter.H和le("/api/v1/", taskRouter)

    fmt.Printf("Server running on port 8000")

    http.ListenAndServe(":8000", mainRouter)

}

// Routes folder    ===================================

package routes

import (
    "fmt"
    "net/http"

    "github.com/sayedulkrm/go-curd-todo/controllers"
)

func TaskRoutes() *http.ServeMux {

    router := http.NewServeMux()

    fmt.Printf("Heyyy Am getting called From TaskRoutes")

    router.H和leFunc("GET /tasks", controllers.GetAllTasks)

    return router

}


// Controllers Folder ===================================

package controllers

import (
    "fmt"
    "net/http"
)

func GetAllTasks(w http.ResponseWriter, r *http.Request) {

    fmt.Printf("Heyyy Am getting called From Controllers")

    w.Write([]byte(`{"message": "working..."}`))

}

I want to make kinda like nested routes.
For example

从前端,我会击中

example.com/api/v1/task

我想在这里抛锚

main.go

`/api/v1` will be added in front of all routes i will create

so for example for order. it will be

`api/v1/orders`

For users:

`api/v1/users`

推荐答案

多路复用器也是一个处理程序.所以基本上,您为您的API创建了一个多路复用器,让它处理不带前缀的请求,并将该处理程序挂载到根处理程序中相应的前缀.

但是,由于您的API多路复用器不知道它所挂载的前缀,因此您需要go 掉这个前缀:

// Create a new ServeMux that is responsible for handling your API requests.
api := http.NewServeMux()
api.Handle("/users", userHandler)
api.Handle("/posts", postHandler)

root := http.NewServeMux()
// A mux is also a http.Handler. So, simply use your api mux
// as a handler for the correct path. HOWEVER, since the subrouter does
// have no clue about the /api/v1/ prefix, you need to strip it off.
// So basically, if a request for /api/v1/users comes in, the /api/v1/ part
// is stripped off and the /users part is passed to the api mux.
// Note that you will have to order the paths from most to least specific.
root.Handle("/api/v1/", http.StripPrefix("/api/v1", api))

100

Go相关问答推荐

如何在Deliverq中高效地传达客户依赖关系?

Go Fiber和HTMX—HX—Trigger header被更改为HX—Trigger,这不是HTMX监听的内容

你能把用户界面文件中的GTK4应用程序窗口添加到GTK4应用程序中吗?

如何在出现错误时停止从通道读取?

如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?

如何根据中间件的请求设置上下文值?获取 go-staticcheck 问题

无效操作:v > max(类型参数 T 与 > 不可比较)

在 GoLang 中对自定义 struct 体数组进行排序

在golang中以JSON格式获取xwwwformurlencoded请求的嵌套键值对

如何将验证器标记添加到嵌套字段

git ls-remote 成功而 go get 失败

如何在 `hashicorp / terraform-exec` 中将 `ApplyConfig` 传递给 `tf.Apply()`?

golang 上基于标头的版本控制

如何使用管理员权限启动 powershell 进程并重定向标准输入 (os.exec)

通过 golang 中的 gremlin-go 库嵌入 gremlin 服务器

go 是否对 struct 使用空间填充之类的东西?

Golang prometheus 显示自定义指标

GoReleaser 和 ssh-agent Github 操作:为什么无法读取用户名...终端提示已禁用?

gob 解码器仅返回数组中的第一个元素

为什么 template.ParseFiles() 没有检测到这个错误?