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
+11.7k Golang : Calculations using complex numbers example
+9.4k Golang : How to get ECDSA curve and parameters data?
+7.7k Golang : Convert(cast) io.Reader type to string
+52.7k Golang : How to get struct field and value by name
+8.9k Golang : Accept any number of function arguments with three dots(...)
+7.8k Golang : Test if an input is an Armstrong number example
+12.2k Golang : Split strings into command line arguments
+9.7k Golang : Copy map(hash table) example
+11.8k Golang : Find age or leap age from date of birth example
+8.8k Golang : Executing and evaluating nested loop in html template
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+27.3k Golang : Find files by name - cross platform example