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
+9.1k Golang : does not implement flag.Value (missing Set method)
+6.6k Golang : When to use make or new?
+7.8k Golang Hello World Example
+4.9k Nginx and PageSpeed build from source CentOS example
+10.1k Golang : Check a web page existence with HEAD request example
+13k Golang : Convert(cast) uintptr to string example
+16.7k Golang : Get own process identifier
+48k Golang : How to convert JSON string to map and slice
+6.9k Web : How to see your website from different countries?
+9.3k Golang : Find the length of big.Int variable example
+5.9k Golang : Function as an argument type example
+8.7k Golang : On lambda, anonymous, inline functions and function literals