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.2k Golang : Scanf function weird error in Windows
+18.8k Golang : How to get struct tag and use field name to retrieve data?
+9.4k Golang : Fix go.exe is not compatible with the version of Windows you're running
+4.3k Golang : Reclaim memory occupied by make() example
+15.9k Golang : Iterate linked list example
+7.6k Golang : Gorilla web tool kit schema example
+34.9k Golang : Convert date or time stamp from string to time.Time type
+12.3k Golang : Activate web camera and broadcast out base64 encoded images
+4.7k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+17.7k Golang : How to make function callback or pass value from function as parameter?
+9.6k Golang : Simple image viewer with Go-GTK