Sunday, March 31, 2019

Enumerate

Golang provide  own enumerate method which is easy and very powerful. I will show you enumerate with in a sec. When i code enumerate in java, believe me i don't understand why it's used? So i never play from this card at that time, but recently when i start learning go. Now i used where it's used and why? Enumerate basically a counting method.

Code 
package main
import (
"fmt"
)
type Weekend int
const (
Sunday Weekend = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func main() {
fmt.Printf("%d",Saturday)
}
If you remember adding color layer then you familiar from this code.  First i create my own type called weekend then used this type. 
Iota-generator: 
Things that confuse you Sunday Weekend = iota & Monday without value. Before we start Iota-generator first we understand monday without type or value
        const (
c = 1 // c : 1
d      // d : 1
)

fmt.Println(c,d)
This is only possible for consts. If you have 2 const and both have same type and same value then you omit next type or value. That's why program execute without error. Result of this problem is "11".
Now time to dive in iota generator. When you assign any const iota which means const initialize with 0, but after iota value increment. That's little different from previous code. As you know enumerate counts. That's iota do for us. Now you know the output of above code.

Saturday, March 30, 2019

Life is all about decisions and repeat your routine

Life is all about Decisions and repeat our routine is an excellent idea which i show you within a moment. Everyday we take so many decisions based on environment conditions, circumstances. Each decision based on cause and effect philosophy. Programmer are wizards who create own world with data and structures. Without data or structure our world is nothing.

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 = 0
for i := 0; i < 5; i++{
count+=1
fmt.Println(count) }
      Little Complex flavour:  
        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"}
for i,v := range arr{
fmt.Printf("iterate:%d value:%s\n", i, v)
}
             i, v := range arr
   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")
}


Friday, March 29, 2019

Adding colorful layer called typed constants

Sometime we need extra tools for advance problems like magician or wizard use their magic gown , advance spells . If You see Harry potter in your childhood then you remember. Without type constants everything fine but if you become a master then you know how to use advances tools. if you interested in more examples then visit

             Advance tools                                          Description
1      Typed Constants                                           solve your problem in your style
2.      Untyped Constants                                      use in scientific research
3.      grpc/protobuf                                              scale out your application (microservice)
4.       goroutines                                                   application optimization
5.       packages                                                      application deploy
There Are few advances tools  which add extra layers or enhance application power. There're so many, when a time come i will introduce them.

Problem is that you convert kelvin into celsius. 
package main
import (
"fmt"
)
type Kelvin float64
type Celsius float64
const (
AbsoluteZeroKelvin Celsius = 273.15
AbsoluteZeroCelsius Kelvin = 273.15
)
func main() {
var k Kelvin = 1
var c Celsius = 1
kelv := KtoC(k)
cels := CtoK(c)
fmt.Println("value of kelvin", kelv , "value of celsius", cels )
}
func KtoC(k Kelvin) Celsius{
return Celsius(k - AbsoluteZeroCelsius)
}
func CtoK(c Celsius) Kelvin{
return Kelvin(AbsoluteZeroKelvin + c)
}
 type Kelvin float64
       First this at package level which means place after import and second it start with Uppercase. If a variable , constants , type declare at package level and start with uppercase then it's means it access other packages too.
 If you write structs in C you know this line create a new type name Kelvin and it's datatype value in float

AbsoluteZeroCelsius Kelvin = 273.15
This is a way to initialize your type, if you want.
Second it's also help you in conversion

func KtoC(k Kelvin) Celsius
In golang func means this is the function
KtoC is the name of the function
k Kelvin here k is a contain-variable of this type
Celsius called return type of a function.

If you look at example functions after return
Celsius(k - AbsoluteZeroCelsius)
this convert back into required datatype.
This is just a flavour of sweet pie,
soon we discuss them in very
details along with other advance tools.

Write an algorithm that take mass of object and
acceleration and tells you
how much force body applied

Thursday, March 28, 2019

House Foundation Complete

Like other person, we have our own toolkit. Programmer are similar to mathematician and magician. Why? Because Programmer play with logic , numbers . Magician play with your brain and mathematician like us numbers and logic. We start our show in a moment.

 bool :  In golang only true and false are allowed. if you use 0 for true and 1 for false or vice                     versa then you have to convert into before play.
 string : In golang string as literal commonly used.
                var s string = "hello"
                   s := "hello"
                 both are valid cases.
 rune : What's is that? Golang provide you new data type for utf8 or unicode data. Rune                    data  also called unicode. You know other languages are not supported by                              computer.  Sometime we need a key that represent a word in any language. With                   the   help of Unicode you can write dozens of words in any language. More                             information  check Unicode Officials   Site. If we want manipulation on the data, first               we need to  convert into numbers or strings   or byte. Go files follow UTF8-Scheme.
          

Golang have few interesting properties 
       1.   At least more than one variables assign values in one time.
               var x int = 1
              var a, b int , int = 0,1
              var c, d int , float64 = 3, 3.44
        2.  Swaps data without extra memory
              var x,y = 3,4
               x,y = y,x
         3. Golang compilation speed much faster than other languages.
         4. Golang commonly used in microservices. 

Constants
Generally const are special kind of variables which value remain constant throughout our program. const pi = 3.14
                const (
           height = 1024
            width = 724
        )
In golang constant are in two types one called untyped constants and type constants
Untyped Constants
      Constants are very much powerful in golang language. Untyped Constants are good choice for numerical problems.
           var f float64 = float64(3.3)
         fmt.Println(f)
         f = float64('a')
        fmt.Println(f)
Short name also categorize in this type because compiler assign type based on value.
         i := 3 // untyped variable not constant

Wednesday, March 27, 2019

Basic Components II

In my previous article, We had seen how variables such as integers in action. 

Float
In Golang float have two classes like integers.
         float32
         float64
Float32, used when accuracy and precision doesn't matter
Float64 used in scientistic simulation where precision of data really matter.
package main
import (
"fmt"
"math"
)
func main() {
var radius float64 = 3.0
fmt.Println(2 * math.Pi *(math.Pow(radius,2))) }
            Math:
        Just like C you can import package before use , that why i import math package.
            math.pi:
       Go provide build-in package math which handle all mathematical operation such as pi, sin-function , cose-function etc.
         math.Pow:
  In Go power function already in math package. Pow take two parameters one number and second degree. Pow return in float64. Because of Pow type we initialize our variable with similar data-type.

If data type are not same then you need to type conversion otherwise it give you error mismatch data type

Byte
 Byte used to store single character. Char replace from byte in golang.Byte doesn't have any class.

Complex
Suppose you're a mathematician and you're solving complex problem. Before solve problem you make sure your toolkit have all tools which you need. Otherwise you can't solve problem. Normally complex data type not used very much because most of our problems based on real numbers.
Complex again have two classes Complex64, Complex 128
package main
import (
"fmt"
)
func main() {
var c complex128 = complex(1,2)
var d complex128 = complex(3,4)
fmt.Println("real:",real(c+d), "imag:",imag(c+d))
}

Pointers
Pointers is also called variable because it contain address of particular variable. Pointers are powerful.

        package main
import (
"fmt"
)
func main() {
x := 100  // x is short name declaration
p := &x   // p also belong to this category
fmt.Printf("Value:%d", *p )
*p++
fmt.Printf("Inc :%d", *p )
}
         Challenge for you
                    You can write a algorithm which point towards x without using short name declaration syntax

Go Foundation

Golang Language is pretty good language. Most of us start our career from C the mother of all Languages. If you build a house you know know to build that.
      Components of house
            1. Variables
            2. Constants
            3. Functions
            4. Conditions
            5. Loops

Variables
      Let start with Variables.
In Golang there're so many types of variables int , float , string, byte , complex , pointers etc.




In go-lang integers exit in five classes. Each class has own properties
      int
      int8
      int16
      int32
      int64
                Int
         integers range start from 0 to 65535.
                 Int8
        integers of this class start from 0 to 2^8.
                Int16
        Similar like int8 it start with 0 to 2^16.
if you had write a code you know integers have two types
         signed and unsigned
There're so many operations on integers such as addition, increment, less than etc. I will show you in a moment.
         var name type = value
         var a int = 0
         var b int = 3
         fmt.Println("c:", a+b)  // 3
             Let me ask you if two different classes of integers operate what's the output
             var a int = 9
             var b int8 = 5
             fmt.Println(a+b)

Please share your answer in a comment section
             In go local variables and global variables are most important. If a variable declare as a local then it's life story little short. While package variables called global variables. They initialize with Upper case Letters. The reason behind that they can access through other files too.
            There's is another flavour in go to initialize local variables but more details in any other topic.
        In  Short name declaration programmer can write name of variable, then ":=" value. This value tells variable type either it's integer, float etc.
         
             a := 0 // int
             c := 3.4    // float32, float64
Gopher's are best
       
             

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Is it language unites or divides the nation

  One way to connect with the heart of the nation: a shared language. Caesar took an oath in the senate... but was it fact or just a clever ...

Achieves