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
+11.9k Golang : convert rune to unicode hexadecimal value and back to rune character
+28.6k Golang : Download file example
+5.7k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+18k Golang : Append content to a file
+9.1k Android Studio : Create custom icons for your application example
+19.7k Golang : Match strings by wildcard patterns with filepath.Match() function
+3.4k Nginx and PageSpeed build from source CentOS example
+42.7k Golang : Marshal and unmarshal json.RawMessage struct example
+9.6k Golang : Fuzzy string search or approximate string matching example
+3.5k Golang : Customize scanner.Scanner to treat dash as part of identifier
+13.8k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+8k Golang : Convert(cast) string to int64