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
+16.9k Golang : How to tell if a file is compressed either gzip or zip ?
+9k Golang : How to check if a string with spaces in between is numeric?
+8k Golang : Oanda bot with Telegram and RSI example
+14.3k Golang : Rename directory
+11.8k Golang : Get remaining text such as id or filename after last segment in URL path
+18.6k Golang : Delete duplicate items from a slice/array
+4.9k Python : Create Whois client or function example
+13.7k Golang : concatenate(combine) strings
+9.9k Golang : How to tokenize source code with text/scanner package?
+8.4k Golang : Set or add headers for many or different handlers
+10.3k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+9.4k Golang : Detect number of active displays and the display's resolution