登录
首页 >  Golang >  Go问答

Go 语言 - 从 GitHub 导入

来源:stackoverflow

时间:2024-04-25 22:39:36 247浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《Go 语言 - 从 GitHub 导入》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我正在学习 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 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. 了解 properly installing Go for your platform
  2. 阅读 getting started tutorial,其中还介绍了如何安装第 3 方软件包并在代码中使用它们

完成这些步骤的时间不会超过 20 分钟,并且几乎可以肯定您将能够在该过程结束时实现您的目标。

到这里,我们也就讲完了《Go 语言 - 从 GitHub 导入》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>