英文:
== 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 < len(modules_arr); i++ {
if modules_arr[i] == "html" { is_html := true }
}
if is_html ==true
{
fmt.Printf("%v", "asdasd")
}
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("%v", "asdasd")
}
The correct code is
if is_html ==true {
fmt.Printf("%v", "asdasd")
}
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{"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.")
}
}
答案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 "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")
}
}
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 (
"fmt"
)
func main() {
isHtml := false
if isHtml {
fmt.Println("isHtml is true")
}
if !isHtml {
fmt.Println("isHtml is false")
}
}
答案5
得分: 0
在Go语言中,无论你声明了什么,你都需要使用.
。所以,如果is_html
为true
,你可以这样写:
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("%T", is_html)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论