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
+18.9k Golang : Get host name or domain name from IP address
+5.1k Golang : Return multiple values from function
+18.9k Golang : Check if directory exist and create if does not exist
+9.6k Golang : Resumable upload to Google Drive(RESTful) example
+9.4k Golang : Detect number of active displays and the display's resolution
+38.8k Golang : How to iterate over a []string(array)
+8.6k Golang : HTTP Routing with Goji example
+8.9k Golang : Write multiple lines or divide string into multiple lines
+6.1k Golang : Detect face in uploaded photo like GPlus
+7.1k Golang : How to convert strange string to JSON with json.MarshalIndent
+23.9k Golang : How to validate URL the right way
+18.6k Golang : Padding data for encryption and un-padding data for decryption