if-statement
path: courses/go-course/if-statement.md
- **fileName**: if-statement
- **Created on**: 2024-08-24 12:47:54
using for check for condition or specific option
func main(){
if x == 20 {
fmt.Println("the is 20 age")
}
else if x == 30 {
fmt.Println("hte user is 30 age")
}
else {
fmt.Println("the user is greater than 30 age")
}
return
}
- this is how to use if in go
Here are some of the comparison operators in Go:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
The initial statement of an if block
An if conditional can have an "initial" statement. The variable(s) created in the initial statement are only defined within the scope of the if body.
if INITIAL_STATEMENT; CONDITION {
}
// example
length := getLength(email)
if length < 1 {
fmt.Println("Email is invalid")
}
if length := getLength(email); length < 1 {
fmt.Println("Email is invalid")
}
switch statment it's better than if statement in one conditiion when the there is alot of if statement now suggested using switch
func main() {
var os string = "windows"
switch os {
case "linux":
fmt.Println("user use linux")
case "windows":
fmt.Println("user use windows")
case "mac":
fmt.Println("user use mac")
case "arch":
fmt.Println("user use arch")
}
return
}
continue:for.md
before:variable-declaration.md