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
+18.1k Golang : How to log each HTTP request to your web server?
+7.4k Golang : How to iterate a slice without using for loop?
+11.6k Swift : Convert (cast) Float to String
+20.8k Golang : Saving private and public key to files
+10k Golang : Sort and reverse sort a slice of integers
+12.1k Golang : Clean formatting/indenting or pretty print JSON result
+22.1k Golang : Use TLS version 1.2 and enforce server security configuration over client
+9.7k Golang : Sort and reverse sort a slice of floats
+5.9k Cash Flow : 50 days to pay your credit card debt
+14.3k Golang : Fix image: unknown format error
+6.3k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+14.2k Golang : syscall.Socket example