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
+8.7k Golang : Get all countries currencies code in JSON format
+12.2k Generate salted password with OpenSSL example
+21.5k Golang : Convert Unix timestamp to UTC timestamp
+7.6k PHP : How to parse ElasticSearch JSON ?
+4.7k PHP : Convert CSV to JSON with YQL example
+15.9k Golang : Capture stdout of a child process and act according to the result
+3.9k MariaDB/MySQL : How to get version information
+5k Javascript : How to replace HTML inside <div>?
+22.4k Golang : Print out struct values in string format
+7.6k Golang : Generate Datamatrix barcode
+25.8k Golang : How to check if a connection to database is still alive ?
+5.9k Golang : Skip or discard items of non-interest when iterating example