英文:
Go compiles declared and not used
问题
我在Go语言中遇到了一些问题,我在tag = true中使用了tag标签。
// 项目 main.go
package main
import (
	"fmt"
)
func main() {
	var m, odd1, odd2, in1, in2 int
	tag := false
	fmt.Scan(&m)
	for i := 0; i < m; i++ {
		fmt.Scan(&in1, &in2)
		odd1 += in1
		odd2 += in2
		if (in1+in2)&1 == 1 {
			tag = true
		}
	}
	if odd1&1 == 0 && odd2&1 == 0 {
		fmt.Print("0")
		return
	}
	if odd1&1 == 0 && odd2&1 == 1 || odd1&1 == 1 && odd2&1 == 0 {
		fmt.Print("1")
		return
	}
	fmt.Print("-1")
}
以上是你提供的代码的翻译。
英文:
I got some problems with Go, I did have used tag  in tag = true
//  project main.go
package main
import (
	"fmt"
)
func main() {
	var m, odd1, odd2, in1, in2 int
	tag := false
	fmt.Scan(&m)
	for i := 0; i < m; i++ {
		fmt.Scan(&in1, &in2)
		odd1 += in1
		odd2 += in2
		if (in1+in2)&1 == 1 {
			tag = true
		}
	}
	if odd1&1 == 0 && odd2&1 == 0 {
		fmt.Print("0")
		return
	}
	if odd1&1 == 0 && odd2&1 == 1 || odd1&1 == 1 && odd2&1 == 0 {
		fmt.Print("1")
		return
	}
	fmt.Print("-1")
}
答案1
得分: 4
“Not used”可以理解为“没有效果”。虽然你将true赋值给tag,但这个赋值没有传播到外部,也没有对函数的结果产生任何影响。
如果你在条件语句中使用tag或者将其作为返回值,那么编译器就不会再报错了。
英文:
'Not used' can be understood as 'has no effect'. While you're assigning true to tag,
this is not propagated to the outside nor has any effect on the result of the function.
If you'd use tag in a condition or return it, then the compiler wouldn't complain anymore.
答案2
得分: 0
你没有使用标签。你又对它进行了赋值。使用意味着它在某个表达式的右边:if tag { 或者 if tag && odd1 && 1 == testVal {
这是Christopher Pfohl的回答。
英文:
You aren't using tag. You're assigning to it again. Using would mean it's on the right hand side of something: if tag { or if tag && odd1 && 1 == testVal {
<sub>This is a Christopher Pfohl answer</sub>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论