TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang If, Else Statements

Use if, else if and else statements to test values. See correct syntax.
If. An if tests a variable in some way. In Go we can build complex if constructs with else-if and else parts. Statements are evaluated until a condition is true.
With initialization statements, we can declare variables in the same line as an if-statement. This syntax leads to clean, understandable code.Built-Ins
An example. We see an if-statement with else-if and else parts. The variable named "value" is initialized to 5. The first if-statement test, >= 5, evaluates to true.

Note: The condition of an if-statement has no surrounding parentheses. The body statements are surrounded by curly braces.

Note 2: In Go the starting curly brace must be placed at the end of the same line. The Gofmt command can help with formatting issues.

Golang program that uses if, else statements package main import "fmt" func main() { value := 10 // Test the value with an if-else construct. if value >= 5 { fmt.Println(5) } else if value >= 15 { fmt.Println(15) // Not reached. } else { fmt.Println("?") // Not reached. } } Output 5
Initialization statements. A variable can be declared and initialized before the condition of an if-statement (on the same line). This syntax makes sense.

Scope: The variable is scoped to the if-statement and its contents. Here we cannot reference "t" outside the if.

Golang program that uses if with initialization statement package main import "fmt" func test() int { return 100 } func main() { // Initialize a variable in an if-statement. // ... Then check it against a constant. if t := test(); t == 100 { fmt.Println(t) } } Output 100
Initialization scope. When we declare a variable at the start of an if-statement, it is scoped to that if-statement (and any else-if and elses).

Undefined: If we use the variable outside its scope, an "undefined" error will occur. This is a compile-time error.

Golang program that uses causes error, undefined variable package main import "fmt" func main() { mult := 10 // The variable "t" can be accessed only in the if-statement. if t := 100 * mult; t >= 200 { fmt.Println(t) } // This causes an error. fmt.Println(t) } Output C:\programs\file.go:16: undefined: t
Unexpected newline error. An "else" in a Go program must be preceded by the closing brace. We cannot put "else" on a separate line. A compile-time error will occur.
Golang program that has unexpected newline error package main import "fmt" func main() { // This program won't compile. value := 10 if value == 20 { fmt.Println(20) } else { fmt.Println(0) } } Output # command-line-arguments syntax error: unexpected semicolon or newline before else syntax error: unexpected {
If, switch performance. In a Go 1.7 benchmark I was unable to show that an integer switch has different performance from an integer if-else chain.Switch: switch versus if
A summary. Ifs are powerful in Go. In most ways they are the same as those in C-like languages. But the initialization statement feature provides a new, scoped variable for the block.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf