Go错误:continue不在循环中

huangapple go评论75阅读模式
英文:

Go Error: continue is not in a loop

问题

我已经写了一个有一个for循环的Go代码,代码如下所示。但是当我构建代码时,我得到了"continue is not within loop"的错误。我不明白为什么会发生这种情况。请帮忙看看。

Go版本:

go version go1.7.5 linux/amd64

完整的代码在下面的链接中:
https://pastebin.com/0ZypMYVK

参考截图
Go错误:continue不在循环中

for k:=0;k < len(args);k++{
    fmt.Println("k is ", k)
    hsCode := args[k]
    lenChk:=checkHashLen(hsCode)
    if lenChk==false {
        fmt.Println("Length should be 32" )
        continue
    }
    codeBytes,_ := json.Marshal(hsCode)
                            
    APIstub.PutState(strconv.FormatInt(makeTimestamp(),10), codeBytes)
    fmt.Println("Added: ", k)     
}

错误Go错误:continue不在循环中

./hashcode.go:88: continue is not in a loop

英文:

I have written go code which has a for loop, code is given below. but when i build the code i get 'continue is not within loop'. i can't understand why this is happening. Kindly help

Go Version:

> go version go1.7.5 linux/amd64

Complete Code at the below link
https://pastebin.com/0ZypMYVK

reference Screenshot
Go错误:continue不在循环中

for k:=0;k &lt; len(args);k++{
    fmt.Println(&quot;k is &quot;, k)
    hsCode := args[k]
    lenChk:=checkHashLen(hsCode)
    if lenChk==false {
        fmt.Println(&quot;Length should be 32&quot; )
        continue
    }
    codeBytes,_ := json.Marshal(hsCode)
                            
    APIstub.PutState(strconv.FormatInt(makeTimestamp(),10), codeBytes)
    fmt.Println(&quot;Added: &quot;, k)     
}

ErrorGo错误:continue不在循环中
> ./hashcode.go:88: continue is not in a loop

答案1

得分: 1

你的问题在这里:

//将单个代码推送到区块上
func (s *SmartContract) pushCode(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

    hsCode := args[0]
    lenChk := checkHashLen(hsCode)
    if lenChk == false {
        fmt.Println("长度应为32")
        continue
    }
    codeBytes, _ := json.Marshal(hsCode)
    APIstub.PutState(strconv.FormatInt(makeTimestamp(), 10), codeBytes)
    return shim.Success(nil)
}

错误解释了出了什么问题。你在不在for循环中使用了关键字continue,而这个函数中没有for循环。

initCodeLedger包含一个for循环,所以你被那个分散了注意力,但是错误给出的行号并不在那个位置,而是在第86/87/88行附近。如果像这样提问,请在play.golang.org上发布代码。

英文:

Your problem is here:

//push single code on the block
func (s *SmartContract) pushCode(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

	hsCode := args[0]
	lenChk := checkHashLen(hsCode)
	if lenChk == false {
		fmt.Println(&quot;Length should be 32&quot;)
		continue
	}
	codeBytes, _ := json.Marshal(hsCode)
	APIstub.PutState(strconv.FormatInt(makeTimestamp(), 10), codeBytes)
	return shim.Success(nil)
}

The error explains what is going wrong. You're using the keyword continue when not in a for loop, this function doesn't contain a for loop.

initCodeLedger contains a for loop, so you are getting distracted by that, but that's not the line no given in the error which is around line 86/87/88. Ideally post code on play.golang.org if asking a question like this.

huangapple
  • 本文由 发表于 2017年8月17日 14:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/45727904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定