Skip to main content

Posts

Showing posts from May, 2019

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Who's am I?

When we are writing problem or build a project, most of time we need to free our resources. When the program release resources ; 1. Program run so fast 2.  Resources allocate to others user too. 3. Handle multitasking. Today I'm writing on Defer function call. Defer function call execute when you free your resources such as closing file, resources lock and unlock, network connection close etc. Defer also use for debugging purpose too. Defer keyword is used. func title(url string) error {   resp, err := http.Get(url)   if err != nil {       return err   }   defer resp.Body.Close()   ct := resp.Header.Get("Content-Type")   if ct != "text/html" && !strings.HasPrefix(ct, "text/html;") {      return fmt.Errorf("%s has type %s, not text/html", url, ct)   }   doc, err := html.Parse(resp.Body)   if err != nil {      return fmt.Errorf("parsing %s as HTML: %v", url, err) } // ...print doc's t

Without this Agent , mission doesn't accomplish?

If you play military games such as Delta force , IGI Project , commandos etc . You familiar about in your team. Each member of team are unique in their skills. Some are snipers , short gun , hackers, communication officer. Now I will introduce golang special agent which are quite helpful in most of time.                 Variadic Function  will show in a moment! Variadic Function:             Variadic  Functions means a function that arguments varing. In variadic function ... called ellipsis.   package main import ( "fmt" ) func main() { values := []int{1,2,3} fmt.Println("Value[0]",seriesAdd(values[0])) fmt.Println("Total", seriesAdd(values...)) } func seriesAdd(x ...int)int{ var count int = 0 for _, v := range x{ count+= v } return count } Conditions :     Before passing to the variadic function array must be slice.                   values := []int{1,2,3} // slice     Always use ellipsis in, when array pass as a argument.   Working

Anonymous Caller Function

You can know about function values but this article also link with my previous blog. Let's start with "Anonymous Caller Function" package main import ( "fmt" ) func squares() func() int { var x int return func() int { x++ return x * x } } func main() { f := squares() fmt.Println(f()) // "1" fmt.Println(f()) // "4" fmt.Println(f()) // "9" fmt.Println(f()) // "16" }   What we already Know ? This code confuse you, don't worry let's start what we already know about functions.    func squares() int{ return x *x}. This function return square which we pass as argument.   f := squares()  function definition receive by f for further processing. What we don't know? func squares() func() int { var x int return func() int { x++ return x * x } }              1.  func() int: this is an anonymous function. Anonymous function are those functions which is similar like in

How Function values are useful?

Golang is interesting language. If you wanna to play then start build your lego empire today. You know how to build homes, shops, trees etc. Someday, I will show you how to build a factories or companies so that our universe look like a real world.  Like ordinary values assign to variables, in golang you can assign function values to your variables, because function values have own type. package main import ( "fmt" ) func main() { f:= square fmt.Println(f(2)) f = underRoot fmt.Println(f(4)) f = add fmt.Println(f(2,2)) // error } func square(x int)int { return x * x } func underRoot(x int)int { return x*1/2 } func add(x, y int)int { return x+y }         Confused ?                f = underRoot                f = add // error Are you confuse in these statement, if yes then square initially assign to f, which take one parameter as a argument. This tells f(2) that f hold square definition and 2 in our case value.             4 [Ok , everything fine] when u

Good Errors Need Your Help

I assume that you have write a program in another language before learning golang. Like c plus plus , Java etc. When a compiler produce exception you can handle it by yourself. In this article i only show you a good errors. package main import ( "fmt" "errors" ) func main() { div, err := divide(0,3) if err != nil{ fmt.Println(div) } fmt.Println(div,err) } func divide(x , y int )(int, error){ if x == 0 { return x/y , nil } return x/y, errors.New("PASS") } If you observe in my above example you see if x equal to zero then it send error while when x not equal to 0 then it send me "Pass" ? errors.New take a string a argument as a parameter and in case any error. This is very productive function when you're working on huge project. Bad Errors also exit in golang but that will come on right time. Programmer handle bad errors like Java developers handle exceptions.

Optimize your function

Everyone want to improve his skills. Programmer usually clean their code after month or when he/she is available. Clean Code means change in existing code that work faster then before and resolve issues which he face before. Within a moment, I will show how to do that?              I f you remember when i write a tic toc game  in golang. That time i explain you functions take only one parameters. Actually that time i don't know how to pass multiple arguments.                        func add(x,y int)int{ /* add functionality*/}                       func move (key string , y  int ) { /**/} In case "a" you observe x, y are two parameters and common data type. When we're in school, we study operation add only available for numbers. While in case "b" move function take two parameters both are different.  Multiple Value Return: In Golang you're function return so many values at ones.                        func moveCheck(x,y int ){            

How to take care naughty functions -> "Recursive"

When we play Recursive Function, we need precautions , because it's recursive in nature. There's a big problem if base condition not valid then it repeat infinite times. Which cause panic in go-lang. Today, I use simple example such as factorial  package main import ( "fmt" ) func main() { i := fact(5) fmt.Println(i) } func fact(n int)int { if n == 1{ return n } return n * fact(n-1) }  Play   If you're a detective then you may notice that in func fact return again fact. This line actually recursive line which repeat over and over, until base condition are true