Functions and Methods

 Function in general 

In programming, a function is a block of code that performs a specific task. Functions allow you to organize your code into logical units and reuse them throughout your program.

Functions in Golang 


In Go, a function is a block of code that performs a specific task and can optionally return a value. Go functions are defined using the func keyword, followed by the function name, a list of parameters, and the function body.

Here is an example of a simple function in Go that takes two integers as arguments and returns their sum:

Go functions can have multiple return values. For example:

In this example, the divmod function takes two integers as arguments and returns their quotient and remainder when one is divided by the other. The function is called with the values 7 and 3, and the returned values are assigned to the variables quotient and remainder.

Anonymous function

Go also supports anonymous functions, which are functions without a named identifier. Anonymous functions can be used as arguments to other functions or assigned to variables.

In this example, the anonymous function is assigned to the variable add, which can then be called like a regular function.


Function closures in Go

In Go, a function closure is a function that encloses the variables in its lexical scope,
allowing the function to access and modify those variables even after the outer
function has returned.

Here is an example of a function closure :
In this example, the add function is a closure that encloses the variable
closure is called, it adds 1 to x and returns the result.

Function closures can be useful in a variety of contexts, such as creating callback
functions or implementing lazy evaluation. They are an important part of the Go
language and are widely used in a variety of programs.

Function closures can be useful in a variety of contexts and situations, such as:

Callback functions: Closures can be used to create callback functions that can
be passed as arguments to other functions. For example, you might use a
closure to create a custom sorting function that can be passed to the sort
package's sort.Slice function.

Lazy evaluation: Closures can be used to implement lazy evaluation, where a
value is only computed when it is needed. This can be useful for optimizing the
performance of programs by avoiding unnecessary computation.

Partial application: Closures can be used to partially apply arguments to a
function, creating a new function with some of the arguments already fixed. This
can be useful for creating functions with a consistent interface that can be used
in different contexts.

Encapsulation: Closures can be used to encapsulate variables and data,
allowing you to create private variables that can only be accessed through the
closure.

Overall, function closures are a powerful and flexible feature of the Go language that
can be useful in a variety of contexts and situations.

Method

In object-oriented programming, a method is a function that is associated with an object or class. Methods are used to define the behavior of an object or class, and can be called on objects of that type to perform the defined behavior.

In Go, a method is a function that is associated with a specific type. Methods are defined using the func keyword, followed by the receiver type, the method name, and the method body. The receiver is a special parameter that represents the object on which the method is called.

Here is an example of a method in Go:

This defines a struct type called Circle with a single field, radius, and a method called Area that calculates and returns the area of the circle. The method is defined using the func keyword, followed by the receiver type (Circle), the method name (Area), and the method body. The receiver is a special parameter that represents the object on which the method is called.

To call the method, you can use the dot notation:

In this example, a Circle object is created with a radius of 5, and the Area method is called on it to calculate the area.

Go also supports pointer receivers, which allow methods to modify the value of the receiver. For example:

In this example, the SetRadius method takes a single argument, radius, and sets the radius field of the receiver to the value of radius. The method is defined with a pointer receiver, which allows it to modify the value of the receiver. The method is called on a Circle object and sets its radius to 10.

Why methods if you have functions

Methods are functions that are associated with a specific type and can be called on
values of that type. They are used to define behavior that is specific to a particular
type and to provide a way to access and manipulate values of that type.

One of the main benefits of using methods is that they allow you to write more
modular and reusable code. By encapsulating behavior in methods, you can define a
set of operations that are specific to a particular type and can be used on values of
that type in a consistent and predictable way.

Methods also allow you to define behavior that is more closely tied to a particular
type, which can make your code easier to understand and maintain. For example, if
you have a type representing a point in a two-dimensional space, you might define
methods to calculate the distance of the point from the origin or to rotate the point
around the origin. These methods would be specific to the Point type and would be
used to manipulate and analyze values of that type.

In Go, methods are defined using a special receiver argument that specifies the type
that the method is associated with. This allows you to define methods that are
specific to a particular type and that can be called on values of that type.

Functions, on the other hand, are standalone pieces of code that can be called with
a set of arguments and return a result. Functions are not associated with a specific
type and can be called on any value or set of values.

Both methods and functions are important features of Go, and you can use them to
write modular and reusable code that can be used to perform a wide variety of tasks.

Interview Questions:
1. What is function in golang
2. What is anonymous function
3. What is function closure
4.What is method in golang
5.Why we need methods if we have functions 

Comments

Popular posts from this blog

Golang Interview Questions Part 1 Theory Top 50 Question

Complete Golang Development setup on Linux (Ubuntu 20.04 )

Best GitHub repositories for Go