gpt4 book ai didi

http - 为什么我不能在 Go 中编写来自单独包的 HTTP 响应?

转载 作者:数据小太阳 更新时间:2023-10-29 03:16:33 25 4
gpt4 key购买 nike

我正在使用 httprouter设置 API 端点,但我在让它正确输出响应时遇到了一些问题。

我目前的主文件:

package main

import (
"fmt"
"github.com/julienschmidt/httprouter"
"infrastructure/routing"
"log"
"net/http"
)

func testPrint(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Test output.")
}

func main() {

fmt.Printf("\n************************************\n")
fmt.Printf("MySite Application Server\n")
fmt.Printf("Version: %s", "Unversioned Development Build\n")
fmt.Printf("************************************\n\n")

router := httprouter.New()

//////Records//////
router.GET("/test", testPrint)
router.GET("/records", routing.RecordList)
router.GET("/records/id/:id", routing.RecordGetById)
router.GET("/records/name/:name", routing.RecordGetByName)
router.GET("/records/search/*search-string", routing.RecordSearch)
router.HEAD("/records/:id", routing.RecordGetExists)
router.POST("/records", routing.RecordCreate)
router.PATCH("/records", routing.RecordUpdate)
router.PUT("/records", routing.RecordUpsert)
router.DELETE("/records/:id", routing.RecordDelete)
router.DELETE("/records/:id/undelete", routing.RecordUndelete)
router.DELETE("/records/:id/purge", routing.RecordPurge)

fmt.Printf("Listening on port 8080...\n")
log.Fatal(http.ListenAndServe(":8080", router))

}

大多数 处理程序函数位于infrastructure/routing 中,但这正是问题开始的地方。目前路由的代码是:

package routing

import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
)

func RecordList(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
//Returns a list of records.
fmt.Fprint(w, "Returned a list of records.")
}

func RecordGetById(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Return a record with the specified ID.
w.Write([]byte("Returned a record by its ID."))
}

func RecordGetByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Returns a record with the specified string in the name field.
w.Write([]byte("Returned a record by its name."))
}

func RecordSearch(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Full text search for the search term provided.
w.Write([]byte("Search results."))
}

func RecordGetExists(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Returns whether a record exists.
w.Write([]byte("Returns whether a record exists. Should usually return 200 or 404."))
}

func RecordCreate(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Creates new records from JSON data in request body.
w.Write([]byte("Creates new records."))
}

func RecordUpdate(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Updates records using JSON data in request body.
w.Write([]byte("Updates existing records."))
}

func RecordUpsert(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Updates existing records and creates new ones using JSON data in request body.
w.Write([]byte("Upserts records."))
}

func RecordDelete(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Marks the record with the specified ID as deleted.
w.Write([]byte("Marks records as deleted."))
}

func RecordUndelete(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Marks the record with the specified ID as undeleted.
w.Write([]byte("Marks deleted records as undeleted."))
}

func RecordPurge(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Permanently deletes the record with the specified ID.
w.Write([]byte("Permanently deletes record with given ID."))
}

当我向 localhost:8080/test 发出请求时,它会调用主包中的处理函数并打印“测试输出”。但是任何其他定义的 URL 都会返回一个 200 OK 状态代码和一个空的(0 字节)响应主体而不是预期的文本。

我很确定这是 某事 与正在传递的 ResponseWriter 对象有关,但我无法弄清楚为什么它对同一函数中的一个函数有效包而不是不同包中的另一个函数,即使这两个函数都传递了编写器的副本。我尝试了几种不同的写入方式,包括 fmt.FPrintfio.WriteStringw.Write,但都无济于事。

最佳答案

好吧,事实证明,当我出于某种原因构建我的代码时,它没有保持目标文件更新。我已经使用 LiteIDE 一段时间了,这是第一次在所有相关包上单独单击构建没有奏效。

我清理了它,然后使用了 Build All,现在它可以工作了。

关于http - 为什么我不能在 Go 中编写来自单独包的 HTTP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32612262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com