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
+9.9k Random number generation with crypto/rand in Go
+30.6k Get client IP Address in Go
+17.6k Golang : Find smallest number in array
+17.8k Golang : delete and modify XML file content
+9.3k Golang : Generate Codabar
+12.8k Golang : Sort and reverse sort a slice of bytes
+19k Golang : Read input from console line
+17.1k Golang : Get number of CPU cores
+5.5k Golang : PGX CopyFrom to insert rows into Postgres database
+82.2k Golang : How to return HTTP status code?
+3.9k Golang : Switch Redis database redis.NewClient