草庐IT

go - 如何在 Go 中设置和访问 "global"记录器?

我确信我遗漏了一些简单、基本的问题,我是新手。假设我不想使用默认记录器,如何设置记录器以便在函数之间共享它?logissue.gopackagemainimport("fmt""github.com/pkg/errors""log""os")vardloglog.Loggerconstlogfilestring="killer.log"constlogprefixstring="LOGTEST:"funcsetupLogger(filename,prefixstring)(*log.Logger,error){out,err:=os.OpenFile(filename,os.O_APP

go - "go test"和 "bazel test"中的不同文件权限错误

如果一个测试想要断言文件权限错误,例如,写入文件系统的根目录,“gotest”返回一个syscall.EACCES错误,而“bazeltest”返回一个系统调用.EPERM。如何让“bazeltest”和“gotest”都通过测试?可以找到一个例子here. 最佳答案 您可以使用bazel--spawn_strategy=standalonetest//...禁用沙箱。我怀疑这会解决这个问题。但是,您可能需要考虑写入/是否是您要测试的行为。如果您需要在不同的操作系统或Docker容器内运行代码,在这种情况下您将获得不同的行为,因此您

unit-testing - 如何修复测试用例中的 "missing type in composite literal"

我正在尝试为函数ReadField()编写测试代码,但我在定义测试用例时遇到了困难。它给出了一个错误“复合文字中缺少类型”。我相信这只是一些语法错误。我已经尝试在函数体之外定义结构体,但它仍然会给出相同的错误。ReadField(string,string,bool)(bool,string)funcTestReadField(t*testing.T){testCases:=[]struct{NamestringInputstruct{FirstStringstringSecondStringstringSomeBoolbool}Expectedstruct{IsValidboolMe

api - GoLang/Mux 语法问题 : if ID, Ok = mux.Vars(r) ["ID"]; !行

我是Golang的新手,我正在阅读某人使用gorilla/mux编写的API代码,我遇到了这段代码。funcheroGet(whttp.ResponseWriter,r*http.Request){varIDstringvarOkboolifID,Ok=mux.Vars(r)["ID"];!Ok{//dosomething}我无法理解Ok在这种特定情况下的作用以及何时触发!Ok。请注意,此函数是GET端点。(r.HandleFunc("/hero/{ID}",heroGet).Methods("GET")) 最佳答案 我假设您使用的

go - 我如何让 "go get"在网络上而不是在我的计算机上查看

我正在尝试为一个项目做贡献,文档告诉我使用这个命令gogetgithub.com/foo/bar但是错误是can'tloadpackage:packagegithub.com/foo/bar:noGofilesin/home/f/go/src/github.com/foo/bar很明显它在我的电脑上看起来是这样,但我如何才能让它从网络上下载呢? 最佳答案 问题是您尝试下载的项目无法构建,因为Go在源路径github.com/foo/bar找不到任何要构建的源文件。但是,包是,已下载,如果您查看$GOPATH/src/github.c

mongodb - 当我尝试调用 API 时,本地服务器返回 "http: panic serving [::1]:52781: runtime error: invalid memory address or nil pointer dereference"

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion我按照指南在这里编写了mongodbAPI:https://www.thepolyglotdeveloper.com/2019/02/developing-restful-api-golang-mongodb-nosql-database/指南的代码运行

go - 为什么是 "err != nil"?

这个问题在这里已经有了答案:Hidingnilvalues,understandingwhyGofailshere(3个答案)关闭7年前。我遇到这样一种情况,变量“errerror”的值只能是“nil”,但在重新分配后断言“(err==nil)==false”。示例代码如下:packagemainimport("fmt""log")typeTestErrorstruct{Messagestring}func(e*TestError)Error()string{returne.Message}funcNewTestError(errerror)*TestError{iferr==nil{

html - go - 调用 "html/template"时没有足够的参数。必须

我在Golang中编写了一个包装函数,用于从多个文件中渲染模板,如下所示:funcRenderTemplate(whttp.ResponseWriter,datainterface{},tmpl...string){cwd,_:=os.Getwd()for_,file:=rangetmpl{file=filepath.Join(cwd,"./view/"+file+".html")}t,err:=template.ParseFiles(tmpl...)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)r

go - 引用另一个结构给出 "undefined"

我有一些非常简单的golang代码:funcmain(){typeconfigstruct{intervalint`mapstructure:"Interval"`statsdPrefixstring`mapstructure:"statsd_prefix"`groups[]group}typegroupstruct{groupstring`mapstructure:"group"`targetPrefixstring`mapstructure:"target_prefix"`targets[]target}}当我运行它时,我得到以下信息:未定义:组我在这里错过了什么?

go - 为什么可以将参数传递给 http.Dir ("."中的类型)?

在http包中,它定义了一个自定义类型(typeDirstring),然后向其添加了一个方法Open(https://golang.org/src/net/http/fs.go#L34)。当在别处调用时,它是由http.Dir(".")完成的,因为它是一个函数或方法。有人可以向我解释为什么以及这里发生了什么吗? 最佳答案 http.Dir(".")是类型转换。它将字符串"."转换为类型http.Dir。 关于go-为什么可以将参数传递给http.Dir("."中的类型)?,我们在Stac