草庐IT

main_body

全部标签

testing - 我应该使用哪种方法来测试 golang 中的 func main()?

我在main.go中有一个函数main()可以完成这项工作,所有其他函数都在它下面(我没有在这里包括它们)。因此,当我为main中包含的所有funcs编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖main函数中的代码。我知道测试库中有一个TestMain函数可以完成这项工作,但我就是不知道如何让它工作,以便测试涵盖funcmain()。下面是我的main()函数,它没有被测试覆盖...funcmain(){c,err:=getConfig()iferr!=nil{log.Fatal(err)}slideshows,err:=getSlideshows(c)ifer

Goroutine 从 main 返回后没有执行完。为什么?

我正在尝试理解golang中的上下文。我从https://golang.org/pkg/context/#example_WithCancel复制了一个例子并稍微改变一下:Playground:https://play.golang.org/p/Aczc2CqcVZRpackagemainimport("context""fmt""time")funcmain(){//gengeneratesintegersinaseparategoroutineand//sendsthemtothereturnedchannel.//Thecallersofgenneedtocancelthecon

go - "non-declaration statement outside function body"错误

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭4年前。Improvethisquestion对于我的httpfunc方法,我不断收到函数主体外的非声明语句错误。我不确定为什么在我修复了一些全局变量后它一直出现。packagemainimport("net/http""github.com/gorilla/websocket")varaudioMessage[]bytevarwhatType

go - 收到响应后延迟 body.close

......resp,err:=httplib.Get(url)iferr!=nil{fmt.Println(err)}deferresp.Body.Close()......是否需要每次都关闭响应体? 最佳答案 引自httppackage的官方文档:Theclientmustclosetheresponsebodywhenfinishedwithit 关于go-收到响应后延迟body.close,我们在StackOverflow上找到一个类似的问题: htt

web - 为什么当我再次运行 main.go 时 View 相同

主.gopackagemainimport("html/template""net/http")vartemplates=template.Must(template.ParseGlob("./templates/*"))funcviewHandler(whttp.ResponseWriter,r*http.Request){err:=templates.ExecuteTemplate(w,"indexPage",nil)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}}funcmain

http - Go 如何从请求 a.body 流式读取到新请求的主体

现在,我用Golang做这样的事情://readallbytesfrombodybytes,err:=ioutil.ReadAll(request.Body)//setthebytesasNewReadertonewrequest.bodyrequest,err:=http.NewRequest(http.MethodPut,url,bytes.NewReader(bytes))但我想从原始body(io.Reader)流读取到新的,而不是通过ReadAll读取所有字节到内存,然后复制到NewRequest。我该如何实现?谢谢。 最佳答案

http - 发送带有 body 的 HEAD http 请求导致 net/http 错误

我想要this异常原因://ErrBodyNotAllowedisreturnedbyResponseWriter.Writecalls//whentheHTTPmethodorresponsecodedoesnotpermita//body.ErrBodyNotAllowed=errors.New("http:requestmethodorresponsestatuscodedoesnotallowbody")当我使用fiddler发送带有正文的HEAD请求时,我收到400/504错误代码,但我在我的应用程序中没有看到任何错误日志。 最佳答案

go - main包中类型struct的名称首字母大小写

所以如果没有导入的话应该是小写的。从主包中导入一些东西很奇怪。但是,根据其他语言的经验,对我来说使用小写字母作为结构/类的第一个看起来很奇怪。什么方式是惯用的? 最佳答案 这不是什么是惯用的问题,如果标识符以小写字母开头,则不会导出,因此只能从声明包(直接)访问,如果标识符以大写字母开头(Unicode类“Lu”),它是导出的,可以从任何包中引用。有关详细信息,请参阅Spec:Exportedidentifiers.这是一个我个人喜欢的设计决定(它保持源代码干净,“它们甚至具有语义效果:名称在包外的可见性取决于它的第一个字符是否大写

go - 语法错误 : Non-declaration statement outside function body, 但所有内容都有声明

这行不通:packagemainvarformatterstring="fmt"import(formatter)funcmain(){fmt.Println(formatter)}我得到:语法错误:函数体之外的非声明语句即使一切都有声明。 最佳答案 根据Gospecification:Eachsourcefileconsistsofapackageclausedefiningthepackagetowhichitbelongs,followedbyapossiblyemptysetofimportdeclarationsthatd

go - main func 上的 reviced channel error 但是 goroutine 中的 if reviced progrenn 没有错误

我通过goroutine将数据发送到channel。当我想在主函数中接收它时,在channel的最后一次接收时出现死锁,packagemainimport("time""fmt")funcsender(chchanstring){ch输出:printresult%schenlprintresult%szhangsprintresult%slisifatalerror:allgoroutinesareasleep-deadlock!goroutine1[chanreceive]:main.main()但是,如果我也在goroutine中替换接收到的进度,则没有错误orrced。有人可以帮