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
+17.3k Golang : Underscore or snake_case to camel case example
+8.2k Golang : Characters limiter example
+10.4k Elastic Search : Return all records (higher than default 10)
+18.1k Golang : Encrypt and decrypt data with TripleDES
+8.8k Golang : Calculate Relative Strength Index(RSI) example
+6.7k PHP : How to parse ElasticSearch JSON ?
+17.3k Golang : Read directory content with os.Open
+10.9k Golang : Generate Code128 barcode
+3.3k Linux/MacOSX : How to symlink a file?
+24.5k Get file path of temporary file in Go
+6.3k Golang : How to check if input string is a word?
+12.2k Golang : concatenate(combine) strings