【问题标题】:== true evaluated but not used== true 评估但未使用
【发布时间】:2014-05-21 19:55:00
【问题描述】:

在代码里面,我尝试做一些操作

is_html := false;

// Check, if HTMl is exist 
for i := 0; i < len(modules_arr); i++ { 
    if modules_arr[i] == "html" { is_html := true }

}

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

但我得到一个错误:

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

【问题讨论】:

  • 请先使用go fmt正确格式化您的代码。
  • is_html := true 应该是is_html = true 注意缺少:
  • 我已经回滚了对更改格式的这个问题的编辑,因为当他修复格式时问题就消失了。

标签: go syntax


【解决方案1】:

在 golang 中,您声明需要使用 .所以,

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

【讨论】:

    【解决方案2】:

    if 语句在 go 的同一行需要 {

    这意味着你不能这样做

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

    正确的代码是

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

    阅读http://golang.org/doc/effective_go.html#semicolons 以获得更好的理解

    此外,如果检查 MyVal == true,您可以使用简短版本:

    if MyVal{
        //do stuff
    }
    

    同样在你的情况下,正确的命名是:IsHtml。您可以使用 golint 打印出样式错误:https://github.com/golang/lint

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

    【讨论】:

      【解决方案3】:

      正如@Dustin 已经指出的那样,它应该是isHtml

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

      package main
      
      import (
          "fmt"
      )
      
      func main() {
          isHtml := false
      
          if isHtml {
              fmt.Println("isHtml is true")
          }
      
          if !isHtml {
              fmt.Println("isHtml is false")
          }
      }
      

      【讨论】:

        【解决方案4】:

        例如,

        package main
        
        func main() {
            modules_arr := []string{"asd", "html"}
            is_html := false
        
            for i := 0; i < len(modules_arr); i++ {
                if modules_arr[i] == "html" {
                    is_html = true
                }
        
            }
            //or
            for _, value := range modules_arr {
                if value == "html" {
                    is_html = true
                }
            }
        
            if is_html {//<- the problem is here! We Can't move this bracket to the next line without errors, but we can leave the expression's second part
                print("its ok.")
            }
        }
        

        【讨论】:

          【解决方案5】:

          例如,

          package main
          
          import "fmt"
          
          func main() {
              modules_arr := []string{"net", "html"}
              is_html := false
              // Check, if HTMl is exist
              for i := 0; i < len(modules_arr); i++ {
                  if modules_arr[i] == "html" {
                      is_html = true
                  }
              }
              if is_html == true {
                  fmt.Printf("%v", "asdasd")
              }
          }
          

          输出:

          asdasd
          

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

          【讨论】:

          • 错误地使用:= 而不是= 是一个令人讨厌的陷阱,很难检测到(幸运的是,如果从未读取过“新变量”,编译器会拒绝)。参见例如go-traps.appspot.com/#collatz-conjecture
          猜你喜欢
          • 2017-12-02
          • 1970-01-01
          • 2017-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多