Wednesday, April 24, 2019

Agents ready to Go on a mission

I refer function as a agents because function work like agents. Sometime different programmer, working on single project or it can be poor choice whole application written in main. That why every language introduce packages and functions.

Packages will be beneficial when programmer use predefined functionalities which already provided by different programmers . Such as images, net packages. If you explore you know images contain , functions, constants, variables and subdirectories. This package only concern with images or pixels. 

Functions are the good option when a programmer work on modules or a blocks or even above scenario. If you play a military games then you know on a mission there are so many agents work together to accomplish mission. Each agent have own tools. Each person have different skills such as some agents short range shooter or some agents have long sniper, some agents are experts in technology etc. You can create any type of function. We build so many functions.

How Functions Work?
Function need a value or values as a argument along with type. Some functions return value after operation or some function return nothing.   
                            func p(x int) {     // print x }
                            func q (x int, y int) int { return x-y}
              
 Pass by Value and Pass by Reference 
            Pass by value:
                  p and q both function called pass by value. In Pass by value copy of variables pass as arguments, that's why change on value doesn't affect on original variables. Functions & variables both are initialize on lexical block. 
           Pass by reference:
                         functions which take pointers, maps, slice , channels as arguments called pass by reference. It can affect on original value after modification.

Functions Writing Ways:
                         func p (x int) int{} 
                         func p (_ int) int{}
                         func p (x int) (int){}
                         func p (x int) (x int){}
Working with other language
                       You can also write like 
                                                     func p (x int) int 
                        this is way to compiler on different language.


Thursday, April 18, 2019

Go handle Json Perfectly

JSON abbreviation "JavaScript Object Notation". There're so many tools for structure data such as xml, Json, asn.1 and google protobuf.

XML and JSON:
writing code in a xml, so called boilerplate. Java and android use xml for project management. While json, is easy as compare to xml. Mostly use json because it's easy to play around and good choice for web application.

Code :
package main
import ( "fmt" "encoding/json")
type Books struct{ Title string  Status bool Writer string  Comments string}


func main() { var Library =[]Books{ {Title:"Lord of Rings the fellowship", Status:false, Writer:"J-J-R-Tolkien",Comments:""}, {Title:"Quantum Aspects of Life",Status: false, Writer:"Paul-C",Comments:""}, {Title:"You were born rich", Status:true, Writer:"Bob Proctor",Comments:"good book"}, } if lib , err := json.MarshalIndent(&Library,""," "); err== nil{ fmt.Printf("%s",lib) } }
Encode library provide so many sub packages json one of them, try to use it the above it just show you a demonstration. You can use Marshal or MarshalIndent for encoding your data in json.

Marshal and MarshalIndent:
  Marshal :
            lib,err := json.Marshal(Library)
        Marshal take a parameter that to be convert in my case that is Library. Json.Marshal
return value and error so you can handle it. This function have one problem encoded data are  not distinguishable. Return value similar to string. 

MarshalIndent:
           lib, err := json.MarshalIndent(&Library, "", " ")
 Again this is similar to previous function but , this function demand address of data , not itself. Second it take two parameters . Just play around

Similar like encoding, you can also decode data by using unMarshal and unMarshalIndent.

I'm thinking i will add google protobuf in my upcoming articles.

 



                                                                              



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)
 }

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