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.7k Golang : Calculate BMI and risk category
+7.6k Golang : Example of how to detect which type of script a word belongs to
+21.5k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+8.3k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+8.8k Golang : Go as a script or running go with shebang/hashbang style
+4.7k Nginx and PageSpeed build from source CentOS example
+15.7k Golang : Update database with GORM example
+13k Golang : Date and Time formatting
+15.5k Golang : Get digits from integer before and after given position example
+14.3k Golang : Convert(cast) int to float example
+6.9k Golang : Squaring elements in array
+5.2k Golang : What is StructTag and how to get StructTag's value?