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
+6.4k Golang : How to make a file read only and set it to writable again?
+10.5k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+4.8k Use systeminfo to find out installed Windows Hotfix(s) or updates
+1.2k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+4.6k Golang : How to get garbage collection data?
+12.3k Golang : Example for RSA package functions
+4.7k Yum Error: no such table: packages
+5.7k Android Studio : Highlight ImageButton when pressed on example
+12.4k Golang : Find files by name - cross platform example
+2.3k Golang : Get S3 or CloudFront object or file information
+15k Golang : Change file read or write permission example
+2.5k Golang : How to stop user from directly running an executable file?