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.4k Golang : Pass database connection to function called from another package and HTTP Handler
+17.3k Golang : Upload/Receive file progress indicator
+20k nginx: [emerg] unknown directive "passenger_enabled"
+10.7k Golang : Simple image viewer with Go-GTK
+24.2k Golang : Change file read or write permission example
+5.3k Unix/Linux : How to find out the hard disk size?
+6.6k Golang : Pat multiplexer routing example
+5k Javascript : Change page title to get viewer attention
+19.7k Golang : Compare floating-point numbers
+5.2k Golang : fmt.Println prints out empty data from struct
+9.4k Javascript : Read/parse JSON data from HTTP response
+17.2k Golang : Clone with pointer and modify value