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
+5.5k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+35.8k Golang : How to split or chunking a file to smaller pieces?
+4.3k Java : Generate multiplication table example
+32k Golang : Copy directory - including sub-directories and files
+5.7k Golang : Function as an argument type example
+14.7k Golang : Save(pipe) HTTP response into a file
+7.6k Golang : Ways to recover memory during run time.
+13.7k Golang : Get current time
+5.8k Javascript : Get operating system and browser information
+5.1k Swift : Convert string array to array example
+5k Javascript : Change page title to get viewer attention
+14.8k Golang : How do I get the local IP (non-loopback) address ?