Reputation: 372
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.
Upvotes: 5
Views: 49979
Reputation: 1
In golang what ever you declared you need to use . so,
if is_html == true {
fmt.Printf("%T", is_html)
}
Upvotes: 0
Reputation: 229058
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
Upvotes: 13
Reputation: 11668
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")
}
}
Upvotes: 1
Reputation: 2551
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.")
}
}
Upvotes: 2
Reputation: 166529
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.
Upvotes: 1