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
+6.2k PageSpeed : Clear or flush cache on web server
+10.3k Golang : Edge detection with Sobel method
+8.9k Golang : How to join strings?
+8k Golang : Mapping Iban to Dunging alphabets
+12.3k Golang : Detect user location with HTML5 geo-location
+10k Golang : Resumable upload to Google Drive(RESTful) example
+14.3k Golang : Check if a file exist or not
+16.2k Golang : How to login and logout with JWT example
+11.7k Get form post value in Go
+16.9k Golang : Gzip file example
+15.7k Golang : Find location by IP address and display with Google Map
+7.1k Nginx : Password protect a directory/folder