Conditions: Conditions help us to take decision based under certain circumstances, such as if a number divisible by 2 at least two times then it's said to be double even.
package main}
import (
"fmt"
)
func main() {
const divisible =2
var even int = 4
if even%divisible == 0{
n := even/divisible
n /= divisible
if n ==1 {
fmt.Println("Doubly Even")
}
}
If you're a developer then you you know this code quite different from other languages syntax. In most languages, you see like this "if (condition)". Yeah it's different but this language compilation power i like most. That's why i like this language.
even%divisible == 0
Always remember taking modules only applicable on integers. Try with float and complex.
Repeat :
golang provide one way for repeat task such iterating array i index. For loop is only choice you have.
For loop in various flavours, we discuss one by one.
Simple For Loop:
var count int = 0Little Complex flavour:
for i := 0; i < 5; i++{
count+=1
fmt.Println(count) }
var arr = [3]int{0,1,2}
for i := 0; i < len(arr); i++{
fmt.Printf("%d\t",arr[i])
}var arr = [3]int{0,1,2}
arr is the name of array
[3] represent size of array
{0,1,2} inside anything in these braces called values
%d represent integer type
%c represent character type
%T represent type
%v represent verbose
%f represent float
%o represent octal
Complex Loop:
var arr = [3]string{"apple","mango","cherry"}i, v := range arr
for i,v := range arr{
fmt.Printf("iterate:%d value:%s\n", i, v)
}
This line give you real flavour of chocolate in your recipe. Always remember range return two values one called iteration and other called value. In further topics , you understand more when you create own function similar like range. If you don't need v then
for i , _ := range arr
"_" placeholder tells compiler value don't need for this operation. Placeholder come from scala language, but this is our secret spell use it wisely.
Switch-Statement:
var key byte = 'x'
switch key {
case 'x':
fmt.Printf("%c",key)
break
case 'o':
fmt.Printf("%c",key)
break
default:
fmt.Println("no found")
}
No comments:
Post a Comment