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
+16.9k Golang : Get download file size
+10.6k Golang : Convert(cast) float to int
+4.9k Get website traffic ranking with Similar Web or Alexa
+4.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+11.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+20.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+4.3k Gogland : Datasource explorer
+21.4k Golang : simulate tail -f or read last line from log file example
+4.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+8k Golang : Generate EAN barcode
+8.7k Golang : List available AWS regions
+9.3k Generate Random number with math/rand in Go