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
+27.8k Golang : Get first few and last few characters from string
+6.9k Golang : Regular Expression find string example
+8.7k Golang : How to check if a website is served via HTTPS
+12.5k Golang : Query string with space symbol %20 in between
+13.4k Golang : Convert(cast) int to float example
+13.2k Golang : Reset buffer example
+4.2k Swift : Convert (cast) Float to Int or Int32 value
+10.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+19.9k Golang : Get password from console input without echo or masked
+4.9k Golang : Struct field tags and what is their purpose?
+7k Findstr command the Grep equivalent for Windows
+13.8k Golang : How to check for empty array string or string?