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
+15.5k Golang : Gzip file example
+5.6k Elasticsearch : Shutdown a local node
+10.1k Google Maps URL parameters configuration
+7.5k Golang : Another camera capture GUI application with GTK and OpenCV
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+15.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+11.6k Golang : Convert int(year) to time.Time type
+8.8k PHP : Get coordinates latitude/longitude from string
+17.2k Golang : Send email with attachment
+8k Golang : Generate EAN barcode
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+7.8k Golang : Write multiple lines or divide string into multiple lines