GoLang==已计算但未使用的true
在代码中,我尝试执行一些操作GoLang==已计算但未使用的true,go,syntax,Go,Syntax,在代码中,我尝试执行一些操作 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") } if语句需要go中同一行的{ 这意味着你不能这样做 if is_ht
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")
}
if语句需要go中同一行的{ 这意味着你不能这样做
if is_html ==true
{
fmt.Printf("%v", "asdasd")
}
正确的代码是
if is_html ==true {
fmt.Printf("%v", "asdasd")
}
阅读以便更好地理解
此外,如果检查MyVal==true,则可以使用短版本:
if MyVal{
//do stuff
}
同样在您的情况下,正确的命名应该是:IsHtml。您可以使用golint打印样式错误:
使用golint的示例:例如
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")
}
}
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.")
}
}
语句is_html:=true
声明了一个新变量,隐藏了在语句is_html:=false
中声明的变量。Writeis_html=true
使用先前声明的变量。例如
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")
}
}
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.")
}
}
主程序包
func main(){
模块\u arr:=[]字符串{“asd”,“html”}
is_html:=false
对于i:=0;i 如果是@Dustin已经指出的{//,它应该是isHtml
在golang你声明你需要使用的东西那么
if is_html == true {
fmt.Printf("%T", is_html)
}
请首先使用go fmt
正确格式化代码is_html:=true
应该是is_html=true
注意,缺少:
我已将此问题的编辑回滚到更改格式的问题,因为在他修复格式时问题消失了。错误地使用:=
,而不是ode>=
是一个很难检测到的棘手问题(幸运的是,如果“新变量”从未被读取,编译器会拒绝)。请参阅示例
if is_html == true {
fmt.Printf("%T", is_html)
}