Tuesday, April 16, 2019

How to Optimize our code efficiency.

In Golang structs are very powerful and common use. In previous article, I show you how to write a struct but this is not efficient way to write. 

type Point struct{ X,Y int }

point := Point{1,2}
                code.1.1 


There are two types struct literals :
                In first type, study above code. Programmer usually avoid this because it's difficult to remember about fields, it's should be in right order. Programmer use this in custom packages and smaller structs types.

                 In second type Programmer like very much , it's more powerful then previous type.
                 anim := GIF.gif{loopCount: nframes}
               
                 If any field value omit then that case value initialize with zero. Structs can also pass by reference [Pointers]. As you know, passing structs as a parameters with the help of pointers. Pointers create copy and use particular copy .

Structs Literals access through pointer.
                     p := &Point{1,2}
                      p := new (Point)
                      *p = Point{1,2}

Structs Comparable 
                 We can also compare structs . "==" & "!=" are common operators for comparison. Structs can also comparable with other types , in that case maps are very helpful.

Embed Field and Anonymous :

      Embed Field :
                  
package main
import ("fmt")
type Point struct{
X,Y int
}
type Circle struct{
Center Point // embed structs-1
Radius int
}
type Wheel struct{
Circle Circle //embed structs-2
Spokes int
}
func main(){
var w Wheel
w.Circle.Center.X =2
w.Circle.Center.Y =2
w.Circle.Radius =3
w.Spokes =3
fmt.Println(w)
} In embed field we can create a structure of any type that structure is the field of next struct. In Wheel or circle you can see this. Anonymous Field: type Circle struct { Point Radius int } type Wheel struct { Circle Spokes int } var w Wheel w.X = 8 // equivalent to w.Circle.Point.X = 8 w.Y = 8 // equivalent to w.Circle.Point.Y = 8 w.Radius = 5 // equivalent to w.Circle.Radius = 5 w.Spokes = 20 Anonymous Field similar like embed field but there 's a great difference. In anonymous field, field have a type but field don't have any name. That's why we called them anonymous field.
package main
import ("fmt")
type Point struct{
X,Y int
}
type Circle struct{
Point //anonymous case#1
Radius int
}
type Wheel struct{
Circle //anonymous case#2
Spokes int
}
func main(){
var wheel Wheel
wheel.Circle.Point.X =4
wheel.Circle.Point.Y =4
wheel.Circle.Radius =4
wheel.Spokes =4
fmt.Println(wheel)
w:= Wheel{Circle{Point{4,4},3},2}
fmt.Printf("%v",w)
}
Because ‘‘anonymous’’ fields do have implicit names, you can’t have two anonymous fields of the same type since their names would conflict. And because the name of the field is implicitly determined by its type, so too is the visibility of the field. You can follow any convention, which you like. This is just a example, upcoming articles . I will discuss you, Why Composition is so important?


           

                   
                   

Thursday, April 11, 2019

Every Object have a structure

If you start your programming career from c , then you know little about structs. Later Struct replace by classes in object oriented programming. However Go inherit from C.

Structs : 
   With the help of structs, programmer can develop any type of structure 
       List
       Stack
       Queue
       Trees
       Graphs

package main
import (
"fmt"
)
type Students struct{
ID int
Name string
}
var record Students
func Id(i int) int{
record.ID = i
return record.ID
}
func Name(name string)string{
record.Name = name
return record.Name
}
func main() {
var id int = 0
var name string = "ali"
fmt.Println("id:", Id(id), "name:", Name(name))
} When we want to create custom type such as Celsius type Celsius float64, same with structs type Students structs except type need data type and in structs. You tells that's struct. Inside Structs such as id, name called field. If field start with upper letter then it will export. var record Students record have fields which students. Instead, we use Students structs you can play with record just like students. User-defined-struct.Id (always"." follow struct). Working: If you run this example then you understand working. First i declare structs "record". then we pass 0 for id and ali for name. Then we print our data. Lets play with pointer:
var r1 *Students = &record (*r1).ID = 0 (*r1).Name = "Ali"
        First i declare new variable name r1. (*r1).id , assign 0 value , not address to id, same with name and then print

type Node struct{
                  Data  int
                  next *Node
 }
   var List Node
            
func create_Node(i int)*Node{
                  List.Data = i
                  List.next = nil
                     return &List
}
              
func main(){
                  n := create_Node(0)
                   fmt.Println(n)
 }

Tuesday, April 9, 2019

Will you friendship with maps?

Maps is a collection of keys and values. [K] represent keys, all the keys have same type V represent values. Just like keys, all values have same types, but both keys and values are not comparable. You can compare keys with other keys.

           make(maps[K]V)  you can create maps like this. 
            
              ages := make(maps[string]int)
              ages["ali"] = 24
         If key doesn't found in a maps then it return zero value. You can applied any operation such as 
       ages["ali"]++ 
       ages["ali"]+= 1
        
 You can delete maps using built-in function delete. If a key doesn't found and you store value then it return panic error. 

package main
import (
"fmt"
)
func main() {
//slices
names := [...]string{"ali","haider","johny"}
name := make([]string, 0, len(names))
for _ , v := range names{
name = append(name, v)
fmt.Println(name)
}
}

make([]string, 0, len(names)):
                        return a new string slice having 3 size capacity.
 append():
                  go provide append function which takes two parameter one slice-array and other is value. name is already a slice-array and empty array. 
"_" : 
         blank identifier is used because I omit "i"

In this example slice-array copy one by one element into another array. 

Slices and Maps values are interchangeable:
package main
import (
"fmt"
)
func main() {
//slices to maps
names := [...]string{"ali","haider","johny"}
person := make(map[string]int)
for _, v := range names{
person[v] = 24
fmt.Println(person)
}
}
     
Sometimes we want to check particular key values is either zero or present.
package main
import (
"fmt"
)
func main() {
//slices to maps
person := make(map[string]int)
person["ali"] = 24
person["haider"] =23
if details , ok := person["haider"]; ok{
fmt.Println(ok)
}
} if details , ok := person["haider"]; ok: ok return bool value. details contain haider age and ok contain true. This condition tells us that haider is a valid key Warning: Always remember comparable two maps must be slice, otherwise comparison is possible. var m = make(map[string]int)

func k(list []string) string { return fmt.Sprintf("%q", list)}
func Add(list []string) {  m[k(list)]++ }
func Count(list []string) int { return m[k(list)] }
Maps are not variables, so you don't it's address Composite Maps: You can create composite maps make(maps[string]maps[string]bool) gopl.io/ch4/graph
var graph = make(map[string]map[string]bool)
func addEdge(from, to string) {
         edges := graph[from]
if edges == nil {
      edges = make(map[string]bool)
      graph[from] = edges }
        edges[to] = true
}
func hasEdge(from, to string) bool { return graph[from][to] }
This is the best composite example which Go Programming Book. Now you can understand maps in deep.

Unleash array secret power

There're so many superheroes in our  world. Today, I will tell you a story of our superhero. I'll show in a moment. 

Array:
When our superpower get his power and protect our world from evils. He only save few peoples due to his limited power called array. Array carry few people at same time. One day in lab, array friend perform series of experiments on array power, so that array save more peoples in short time.
Slice:
Array friend, one day tell array that I found a new team member that can also help you to save this world from evil. His name is slice. Slice have power that it can save 2x people. Second slice can shrink and grow according to need. 
Array become slice if the array don't have fixed size, Slice contain pointer which point to the array which is not first element. Slice have length and capacity.

package main
import (
"fmt"
)
func main() {

week := [...]string{1: "Mon", 2: "Tue", 3: "Wed", 4: "Thr", 5:"Fri", 6: "Sat", 7:"Sun"}
weekend := week[:]
start := week[0:2]
end := week[len(week)-1]
work :=  week[0:5]
fmt.Println(work)
holiday := week[6:7]

fmt.Println("holi:",holiday) fmt.Println("week:",weekend)
fmt.Println("start:",start)
fmt.Println("end:",end)

// extend capacity
work_c :=  week[0:6]
fmt.Println("capacity work:",work_c)
holiday_c := week[6:8]
fmt.Println("capacity holi:",holiday_c)

// panic error
p := week[0:10]
}
Slices are not comparable with other slice "==".In slices  "..." represent dynamic size.
Slice == len(0) then it's mean it's empty array.  if 0 <= i <= j < cap(slice ) then create new slice. You can add more value of same type in slice, by using append build-in function.
           
                    make([]T, len)
                    make([]T, len, capacity)
 Make:
Make is a special command used to create a new type of slice and ,maps. In First case len represent length of array. In second case it take three parameters one array which slice, len and capacity. Increase in capacity enhances slice power.  
 
Next We discover maps powers and how it can help us in our mission. What're there strengths and weakness. 
 

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

A prince learned his lesson sometimes on his expenses or sometime on others

 A prince sometimes moves in an uncertain path or attitude with zealous energy that he had lost many seige battles without knowing what upco...

Achieves