Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Functions in Go

8. Functions in Go

Functions are a crucial concept in Go and in programming in general. They allow you to encapsulate blocks of code that perform specific tasks, making your code modular and easier to manage. Let's explore functions in detail with examples:

Function Declaration:

In Go, you declare a function using the 'func'  keyword, followed by the function name, parameter list, return type, and function body.

func functionName(parameters) returnType {
// Function body
// ...
return result
}

 

Example: A Simple Function:
func add(a, b int) int {
return a + b
}

 

Function Parameters:

Parameters are inputs to a function. You specify their types after the parameter name.

Example: Function with Parameters:
func greet(name string) {
    fmt.Printf("Hello, %s!\n", name)
}

 

Return Values:

A function can return one or more values. You specify the return type(s) after the parameter list.

Example: Function with Return Value:
func multiply(a, b int) int {
return a * b
}

 

Multiple Return Values:

Go allows functions to return multiple values, which is useful for returning both a result and an error value, for example.

Example: Function with Multiple Return Values:
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}

 

Named Return Values:

You can specify named return values in a function signature. When a return statement is encountered, the named values are automatically returned.

Example: Function with Named Return Value:
func divide(a, b int) (result int, err error) {
if b == 0 {
err = errors.New("division by zero")
return // "result" is automatically set to 0
}
result = a / b
return
}

 

Variadic Functions:

Variadic functions accept a variable number of arguments of a certain type.

Example: Variadic Function:
func sum(numbers ...int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}

 

Anonymous Functions (Closures):

You can declare functions without names, known as anonymous functions or closures. They can be assigned to variables and used as arguments to other functions.

Example: Anonymous Function:
add := func(a, b int) int {
return a + b
}
result := add(3, 5)

 

Function as Parameter:

You can pass functions as arguments to other functions.

Example: Function as Parameter:
func applyOperation(a, b int, operation func(int, int) int) int {
return operation(a, b)
}

 

Function as Return Value:

Functions can also be returned from other functions.

Example: Function as Return Value:
func getMultiplier(factor int) func(int) int {
return func(x int) int {
return factor * x
}
}

 

Defer Statement:

The 'defer' statement postpones the execution of a function until the surrounding function returns.

Example: Defer Statement:
func main() {
defer fmt.Println("This will be printed last.")
fmt.Println("This will be printed first.")
}

 

Functions are the building blocks of your code. They allow you to structure your logic, reuse code, and make your programs more organized and readable. By understanding how to declare, use, and manipulate functions, you can create complex applications with modular and maintainable code.