I'm learning Go and have been given a "lab sheet" to learn some fundamentals which I'm struggling with. We've been told to enter this code to "Create a reply with POST": (despite not knowing what this means)
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()
}
It also says that for this to run the below commands have to be run:
$ go get github.com/gorilla/mux
$ go run outoffice.go
I run the first command fine but the second says "no required module provides package github.com/gorilla/mux: working directory is not part of a module"
Furthermore, this is the error I get when hovering over the github.com/gorilla/mux line inside Visual Studio:
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
I'm not sure the effect it has (if any) but I'm using Powershell and Visual Studio Code to code.
I'm completely new to Go and the Powershell so any help would be great! Thank you
I recommend going through the following path, using official documentation pages:
It should take you no more than 20 minutes to go through these steps, and it's almost certain that you'll be able to accomplish your goal by the end of the process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.