簡體   English   中英

Go 語言 - 從 GitHub 導入

[英]Go Language - Import from GitHub

我正在學習 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

我運行第一個命令很好,但第二個說“不需要模塊提供 package github.com/gorilla/mux:工作目錄不是模塊的一部分”

此外,這是我在 Visual Studio 內的 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 Studio Code 進行編碼。

我對 Go 和 Powershell 完全陌生,所以任何幫助都會很棒! 謝謝

我建議通過以下路徑,使用官方文檔頁面:

  1. 閱讀有關為您的平台正確安裝 Go 的信息
  2. 閱讀入門教程,它還告訴您如何安裝 3rd-party 包並在您的代碼中使用它們

通過這些步驟到 go 應該花費不超過 20 分鍾的時間,並且幾乎可以肯定,您將能夠在該過程結束時實現您的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM