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
+6k Golang : Fibonacci number generator examples
+5.6k Golang : Humanize and Titleize functions
+5.2k Golang : How to solve "too many .rsrc sections" error?
+7.3k Swift : Convert (cast) Character to Integer?
+13.9k Golang : How to get Unix file descriptor for console and file
+44.1k Golang : Read tab delimited file with encoding/csv package
+14.9k Golang : Get file permission
+16k Golang : Defer function inside init()
+7.4k Golang : On lambda, anonymous, inline functions and function literals
+6k Golang : Squaring elements in array
+5.1k Golang : Dealing with backquote
+18.9k Nginx + FastCGI + Go Setup.