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
+13.9k Golang : Google Drive API upload and rename example
+10.3k Golang : Convert file unix timestamp to UTC time example
+9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+11.4k Use systeminfo to find out installed Windows Hotfix(s) or updates
+13.1k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+16.5k Golang : File path independent of Operating System
+24.4k Golang : GORM read from database example
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+7.2k Golang : File system scanning
+20.5k Android Studio : AlertDialog and EditText to get user string input example
+12.4k Golang : Extract part of string with regular expression
+8.7k Golang : Take screen shot of browser with JQuery example