Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== true evaluated but not used

Tags:

syntax

go

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.
like image 843
CETb Avatar asked Apr 08 '14 21:04

CETb


4 Answers

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

like image 168
nos Avatar answered Oct 22 '22 12:10

nos


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.")
    }
}
like image 29
mraron Avatar answered Oct 22 '22 12:10

mraron


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.

like image 1
peterSO Avatar answered Oct 22 '22 12:10

peterSO


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")
    }
}
like image 1
030 Avatar answered Oct 22 '22 13:10

030