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
+19.2k Golang : Delete item from slice based on index/key position
+12.8k Golang : Add ASCII art to command line application launching process
+12.5k Golang : HTTP response JSON encoded data
+7.5k Golang : Process json data with Jason package
+5.4k Golang : What is StructTag and how to get StructTag's value?
+15.3k Golang : Delete certain files in a directory
+21.2k Golang : Convert(cast) string to rune and back to string example
+10.1k Golang : Identifying Golang HTTP client request
+6.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+8.3k Golang : Emulate NumPy way of creating matrix example
+17.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography