== true 被评估但未被使用

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

== true evaluated but not used

问题

在代码中,我尝试进行一些操作。

  1. is_html := false;
  2. // 检查是否存在 HTML
  3. for i := 0; i < len(modules_arr); i++ {
  4. if modules_arr[i] == "html" { is_html := true }
  5. }
  6. if is_html == true
  7. {
  8. fmt.Printf("%v", "asdasd")
  9. }

但是我遇到了一个错误:

  1. ./api.go:26: if 语句中缺少条件
  2. ./api.go:26: is_html == true 被计算但未使用
  3. 错误:进程以代码 2 退出。
英文:

Inside the code, I try to do some operations

  1. is_html := false;
  2. // Check, if HTMl is exist
  3. for i := 0; i &lt; len(modules_arr); i++ {
  4. if modules_arr[i] == &quot;html&quot; { is_html := true }
  5. }
  6. if is_html ==true
  7. {
  8. fmt.Printf(&quot;%v&quot;, &quot;asdasd&quot;)
  9. }

But I get an error:

  1. ./api.go:26: missing condition in if statement
  2. ./api.go:26: is_html == true evaluated but not used
  3. Error: process exited with code 2.

答案1

得分: 13

在Go语言中,if语句的左括号"{"需要与if关键字在同一行。

这意味着你不能这样写:

  1. if is_html == true
  2. {
  3. fmt.Printf("%v", "asdasd")
  4. }

正确的写法是:

  1. if is_html == true {
  2. fmt.Printf("%v", "asdasd")
  3. }

阅读http://golang.org/doc/effective_go.html#semicolons可以更好地理解这个规则。

另外,如果你只是检查MyVal是否为true,可以使用简化的写法:

  1. if MyVal {
  2. //执行操作
  3. }

在你的情况下,正确的命名应该是:IsHtml。你可以使用golint来检查代码风格错误:https://github.com/golang/lint

使用golint的示例:https://www.golangprograms.com/media/wysiwyg/name.JPG

英文:

if statements needs the { on the same line in go

This means you cannot do

  1. if is_html ==true
  2. {
  3. fmt.Printf(&quot;%v&quot;, &quot;asdasd&quot;)
  4. }

The correct code is

  1. if is_html ==true {
  2. fmt.Printf(&quot;%v&quot;, &quot;asdasd&quot;)
  3. }

Read http://golang.org/doc/effective_go.html#semicolons for a better understanding

Also if checking if MyVal == true, you can use the short version:

  1. if MyVal{
  2. //do stuff
  3. }

Also in your case, the correct naming would be : IsHtml. You can use golint to print out style mistakes: https://github.com/golang/lint

Example of using golint : https://www.golangprograms.com/media/wysiwyg/name.JPG

答案2

得分: 2

例如,

  1. package main
  2. func main() {
  3. modules_arr := []string{"asd", "html"}
  4. is_html := false
  5. for i := 0; i < len(modules_arr); i++ {
  6. if modules_arr[i] == "html" {
  7. is_html = true
  8. }
  9. }
  10. //或者
  11. for _, value := range modules_arr {
  12. if value == "html" {
  13. is_html = true
  14. }
  15. }
  16. if is_html {//<- 这里有问题!我们不能将这个括号移到下一行,否则会出错,但我们可以保留表达式的第二部分
  17. print("没问题。")
  18. }
  19. }
英文:

For example,

  1. package main
  2. func main() {
  3. modules_arr := []string{&quot;asd&quot;, &quot;html&quot;}
  4. is_html := false
  5. for i := 0; i &lt; len(modules_arr); i++ {
  6. if modules_arr[i] == &quot;html&quot; {
  7. is_html = true
  8. }
  9. }
  10. //or
  11. for _, value := range modules_arr {
  12. if value == &quot;html&quot; {
  13. is_html = true
  14. }
  15. }
  16. if is_html {//&lt;- the problem is here! We Can&#39;t move this bracket to the next line without errors, but we can leave the expression&#39;s second part
  17. print(&quot;its ok.&quot;)
  18. }
  19. }

答案3

得分: 1

例如,

  1. package main
  2. import "fmt"
  3. func main() {
  4. modules_arr := []string{"net", "html"}
  5. is_html := false
  6. // 检查是否存在 HTML
  7. for i := 0; i < len(modules_arr); i++ {
  8. if modules_arr[i] == "html" {
  9. is_html = true
  10. }
  11. }
  12. if is_html == true {
  13. fmt.Printf("%v", "asdasd")
  14. }
  15. }

输出:

  1. asdasd

语句 is_html := true 声明了一个新变量,隐藏了语句 is_html := false 中声明的变量。要使用先前声明的变量,请写成 is_html = true

英文:

For example,

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. modules_arr := []string{&quot;net&quot;, &quot;html&quot;}
  5. is_html := false
  6. // Check, if HTMl is exist
  7. for i := 0; i &lt; len(modules_arr); i++ {
  8. if modules_arr[i] == &quot;html&quot; {
  9. is_html = true
  10. }
  11. }
  12. if is_html == true {
  13. fmt.Printf(&quot;%v&quot;, &quot;asdasd&quot;)
  14. }
  15. }

Output:

  1. asdasd

The statement is_html := true declared a new variable, hiding the variable declared in the statement is_html := false. Write is_html = true to use the previously declared variable.

答案4

得分: 1

如@Dustin已经指出,应该是isHtml

https://play.golang.org/p/Whr4jJs_ZQG

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. isHtml := false
  7. if isHtml {
  8. fmt.Println("isHtml为真")
  9. }
  10. if !isHtml {
  11. fmt.Println("isHtml为假")
  12. }
  13. }
英文:

As @Dustin already indicated, it should be isHtml.

https://play.golang.org/p/Whr4jJs_ZQG

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. isHtml := false
  7. if isHtml {
  8. fmt.Println(&quot;isHtml is true&quot;)
  9. }
  10. if !isHtml {
  11. fmt.Println(&quot;isHtml is false&quot;)
  12. }
  13. }

答案5

得分: 0

在Go语言中,无论你声明了什么,你都需要使用.。所以,如果is_htmltrue,你可以这样写:

  1. if is_html == true {
  2. fmt.Printf("%T", is_html)
  3. }

请注意,这里的fmt.Printf函数用于打印输出。

英文:

In golang what ever you declared you need to use . so,

  1. if is_html == true {
  2. fmt.Printf(&quot;%T&quot;, is_html)
  3. }

huangapple
  • 本文由 发表于 2014年4月9日 05:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/22948567.html
匿名

发表评论

匿名网友

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

确定