我正在学习Go,并得到了一份“实验室工作表”,以学习一些我正在努力学习的基础知识。我们被告知输入以下代码“用POST创建回复”:(尽管不知道这意味着什么)
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
type Reply struct {
Summary string
}
var replies map[string]Reply
func Create(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
user := vars["user"]
decoder := json.NewDecoder(r.Body)
var reply Reply
if err := decoder.Decode(&reply); err == nil {
w.WriteHeader(http.StatusCreated)
replies[user] = reply
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func handleRequests() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/outoffice/{user}", Create).Methods("POST")
log.Fatal(http.ListenAndServe(":8888", router))
}
func main() {
replies = make(map[string]Reply)
handleRequests()
}它还指出,要运行以下命令,必须运行以下命令:
$ go get github.com/gorilla/mux
$ go run outoffice.go我运行第一个命令很好,但是第二个命令说:“没有必需的模块提供包github.com/gorilla/mux:工作目录不是模块的一部分”
此外,当我在Visual中的github.com/gorilla/mux行上徘徊时,会遇到这样的错误:
could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of
C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
C\src\github.com\gorilla\mux (from $GOPATH)
\go-workspace\src\github.com\gorilla\mux (from $GOPATH))compilerBrokenImport我不确定它的效果(如果有的话),但我正在使用Powershell和Visual代码进行编码。
我刚开始使用Powershell,所以任何帮助都是很棒的!谢谢
https://stackoverflow.com/questions/66566485
复制相似问题