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
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+5.2k Golang : Process non-XML/JSON formatted ASCII text file example
+31.3k Golang : Smarter Error Handling with strings.Contains()
+7k Findstr command the Grep equivalent for Windows
+17.7k Golang : convert int to string
+12.3k Golang : Strings comparison
+8.3k Golang : How to get ECDSA curve and parameters data?
+6.8k Javascript : Put image into Chrome browser's console
+11.8k Android Studio : Password input and reveal password example
+7.9k Golang : Get SPF and DMARC from email headers to fight spam
+5.7k Get Facebook friends working in same company
+53.2k Golang : Unmarshal JSON from http response