Golang : If else example and common mistake
Maybe I'm so used to PHP before moving to Golang that I keep making the same mistake with IF-ELSE-THEN statement in Golang.
This is a good example of IF-ELSE
package main
import (
"fmt"
"os"
)
func main() {
if os.Args[1] == "Hello" {
fmt.Println("Hello World")
} else {
fmt.Println("GoodBye World")
}
}
and bad example, which WILL NOT compile because the {
is one line below 'IF`
package main
import (
"fmt"
"os"
)
func main() {
if os.Args[1] == "Hello"
{ // will not compile
fmt.Println("Hello World")
} else {
fmt.Println("GoodBye World")
}
}
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.2k CodeIgniter : form input set_value cause " to become & quot
+5.9k Golang : How to check if a file is hidden?
+3.5k Golang : Get curl -I or head data from URL example
+5k Golang : Simple client-server HMAC authentication without SSL example
+3.2k Golang : Temperatures conversion example
+3.6k Golang : Multiplexer with net/http and map
+15k Golang : Get file last modified date and time
+4.2k Golang : Serving HTTP and Websocket from different ports in a program example
+2.1k Mac/Linux/Windows : Get CPU information from command line
+6.2k Golang : Overwrite previous output with count down timer
+6.3k Javascript : Prompt confirmation before exit
+4.4k Golang : How to use Gorilla webtoolkit context package properly