== true 被评估但未被使用

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

== true evaluated but not used

问题

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

is_html := false;

// 检查是否存在 HTML
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: if 语句中缺少条件
./api.go:26: is_html == true 被计算但未使用
错误:进程以代码 2 退出。
英文:

Inside the code, I try to do some operations

is_html := false;

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

}

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

But I get an error:

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

答案1

得分: 13

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

这意味着你不能这样写:

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 {
    //执行操作
}

在你的情况下,正确的命名应该是: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

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

The correct code is

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

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:

if MyVal{
    //do stuff
}

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

例如,

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
        }

    }
    //或者
    for _, value := range modules_arr {
        if value == "html" {
            is_html = true
        }
    }

    if is_html {//<- 这里有问题!我们不能将这个括号移到下一行,否则会出错,但我们可以保留表达式的第二部分
        print("没问题。")
    }
}
英文:

For example,

package main

func main() {
    modules_arr := []string{&quot;asd&quot;, &quot;html&quot;}
	is_html := false

	for i := 0; i &lt; len(modules_arr); i++ {
		if modules_arr[i] == &quot;html&quot; {
			is_html = true
		}

	}
    //or
	for _, value := range modules_arr {
		if value == &quot;html&quot; {
			is_html = true
		}
	}

	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
		print(&quot;its ok.&quot;)
	}
}

答案3

得分: 1

例如,

package main

import "fmt"

func main() {
    modules_arr := []string{"net", "html"}
    is_html := false
    // 检查是否存在 HTML
    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

英文:

For example,

package main

import &quot;fmt&quot;

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

Output:

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

package main

import (
	"fmt"
)

func main() {
	isHtml := false

	if isHtml {
		fmt.Println("isHtml为真")
	}

	if !isHtml {
		fmt.Println("isHtml为假")
	}
}
英文:

As @Dustin already indicated, it should be isHtml.

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

package main

import (
	&quot;fmt&quot;
)

func main() {
	isHtml := false

	if isHtml {
		fmt.Println(&quot;isHtml is true&quot;)
	}

	if !isHtml {
		fmt.Println(&quot;isHtml is false&quot;)
	}
}

答案5

得分: 0

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

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

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

英文:

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

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

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:

确定