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
+4.5k Unix/Linux : Get reboot history or check when was the last reboot date
+4.8k Golang & Javascript : How to save cropped image to file on server
+26.5k Golang : Detect (OS) Operating System
+6.5k Android Studio : Rating bar example
+3.5k Python : Find out the variable type and determine the type with simple test
+14.2k Golang : Read integer from file into array
+15.2k Golang : Find file size(disk usage) with filepath.Walk
+10.4k Golang : Determine if time variables have same calendar day
+6.1k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+12.1k Golang : convert rune to unicode hexadecimal value and back to rune character
+8.7k Golang : Convert file content to Hex
+16.2k Golang : Get download file size