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
+8.1k Golang : Find relative luminance or color brightness
+9.6k Golang : Format strings to SEO friendly URL example
+22.6k Golang : Round float to precision example
+21k Golang : Sort and reverse sort a slice of strings
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode
+7.8k Javascript : How to check a browser's Do Not Track status?
+22k Golang : Match strings by wildcard patterns with filepath.Match() function
+21.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+4.7k Javascript : How to get width and height of a div?
+18.1k Golang : Get command line arguments
+12.5k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+15.1k Golang : Get timezone offset from date or timestamp