草庐IT

use_for_related_fields

全部标签

go - 使用for循环遍历 channel 时出现Goroutine死锁

我正在尝试练习goroutine和channel,我在调用gorouting和传递channel时遇到问题。goroutine将数据推送到channel中,然后主线程将打印元素。我已经使用for循环来打印内容但是得到了。fatalerror:allgoroutinesareasleep-deadlock!21goroutine1[chanreceive]:main.main()packagemainimport"fmt"funcsmallThread(aint,cchanint){c编辑:使用WaitGroup:funcsmallThread(aint,cchanint,w*sync.

Golang 项目 Travis CI Build 失败,错误为 `Makefile:15: recipe for target ' test' failed`

关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。这个问题似乎与helpcenter中定义的范围内的编程无关。.关闭3年前。Improvethisquestion我已经为我的Github项目设置了travisbuildCaptain.travis.yml的内容是:language:gogo:-1.12.xscript:makeenv:-GO111MODULE=onGOPROXY=https://proxy.golang.org我的Makefile的内容是:#GoparametersBINARY_FOLDER=./binGOCMD=goGOBUILD=

go - 错误 : "declared and not used" even if I assign to it

我找不到为什么下面的代码给出编译错误“alivedeclaredandnotused”。funcping(ipstring){varalivebool_,err:=exec.Command("ping","-n1","-w1000",ip).Output()iferr!=nil{alive=false}else{alive=true}} 最佳答案 您看到的编译错误正是正在发生的事情。varalivebool未使用。您声明它并为其分配一个值,但您永远不会对它做任何事情。这是对将运行的代码的playground友好修改:packagem

go - 主.go :9: use of package str without selector

我在TourofGo的解释器中有以下内容:packagemainimport("golang.org/x/tour/wc"str"strings")funcWordCount(sstring)map[string]int{results:=make(map[str]int)words:=str.Fields(s)returnmap[string]int{"x":1}}//funcmain(){//wc.Test(WordCount)//}这是基于https://tour.golang.org/moretypes/23我的错误是tmp/sandbox169629521/main.go:9

unit-testing - httptest.NewRequest 与 http.NewRequest : which one to use in tests and why?

Golang有这两个相似的库http和httptest并且它们都有NewRequest函数。如果http.NewRequest能做到这一切,为什么我们还需要httptest.NewRequest?如果我需要为我的测试创建多部分/多形式请求,我需要使用哪一个? 最佳答案 如文档中所示,httptest.NewRequest“返回一个新的传入服务器请求,适合传递给http.Handler进行测试”,而http.NewRequest“返回适合与Client.Do或Transport.RoundTrip一起使用的请求。”因此,如果您在单元测

go - 检查 channel 长度在 `for{ len(c) }` 中变得无响应

以下程序从不打印“Full”。在fmt.Println(len(choke))未注释的情况下,程序在channel已满时输出“Full”。packagemainimport("fmt")funcmain(){choke:=make(chanstring,150000)gofunc(){fori:=0;i=150000{fmt.Println("Full")}}}@tim-heckman在OP中解释了此行为的原因.如何在不使用热循环的情况下检测channel已满? 最佳答案 在写入端使用select语句。如果有缓冲区可用或有接收者等待

go - 为什么 'var' 不能用在 Go 的 for 循环中?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭3年前。Improvethisquestion我是Go的新手,有一个问题。据我所知,以下两个片段基本相同。i:=0vari=0但是为什么var语句不能用在for循环中呢?forvari=0;i将会出现语法错误:vardeclarationnotallowedinforinitializergo为什么要这样设计?里面有什么思想吗?如果有人能告诉我,我将不胜感激。

arrays - 这个使用范围的简单 For 循环如何以及为什么在 Go 中打印一个简单的 3D 数组?

我尝试以这种方式使用range函数来打印电路板并且它确实有效,但我无法解决为什么?行数、列数如何计算?packagemainimport"fmt"funcmain(){varboard[2][2][2]stringforrow:=rangeboard{forcolumn:=rangeboard{forthird:=rangeboard{fmt.Print(row,column,third)fmt.Println()}}}} 最佳答案 您的代码并没有按照您的想法行事。您在每个循环中迭代顶级数组。每个数组恰好有2个元素这一事实给了你成功

for-loop - for循环中的golang范围是否保证只被评估一次?

在我正在开发的软件中,我看到许多循环如下所示:for_,i:=rangestrings.Split(s,","){...}我想确保范围只被评估一次,因为拆分字符串总是代价高昂。golang的文档中有没有这方面的规定? 最佳答案 https://golang.org/ref/specTherangeexpressionxisevaluatedoncebeforebeginningtheloop,withoneexception:ifatmostoneiterationvariableispresentandlen(x)isconsta

go - 我收到错误 fatal error : runtime: out of memory while downloading video using 'go-ipfs-api'

我使用go-ipfs-api从ipfs下载了一个大文件,web访问下载。我收到一个fatalerror:runtime:outofmemory.如何修改我的代码?funcmain(){http.HandleFunc("/",download)http.ListenAndServe(":8080",nil)}funcdownload(whttp.ResponseWriter,r*http.Request){client:=shell.NewShell("http://127.0.0.1:5001")fd,err:=client.Cat("QmTcj7SfRf4vnLnCqnxMT7kut