
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Functions as Values
Functions as Values
Go programming language provides the flexibility to create functions on the fly and use them as values. In the following example, we've initialized a variable with a function definition. Purpose of this function variable is just to use inbuilt math.sqrt() function.
Example
For example −
package main import ("fmt" "math") func main(){ /* declare a function variable */ getSquareRoot := func(x float64) float64 { return math.Sqrt(x) } /* use the function */ fmt.Println(getSquareRoot(9)) }
When the above code is compiled and executed, it produces the following result −
3
In Go language, the functions can be assigned to variables, passed as arguments to other functions, or returned as values from functions. Let's understand each concept in detail.
Assigning a Function to a Variable
Go language allows you to assign a function directly to a variable. The function should have a return type.
Example
package main import "fmt" // Function func addTwoNumbers(a int, b int) int { return a + b } func main() { // Assign the function to a variable sum := addTwoNumbers // Here, we are calling the function // using the variable result := sum(100, 200) fmt.Println("Sum:", result) }
When the above code is compiled and executed, it produces the following result −
Sum: 300
Passing a Function as an Argument
You can pass a function to a function as an argument; you just need to use the function declaration as an argument.
Example
package main import "fmt" // Passing a Function as an Argument func calculation(x int, y int, op func(int, int) int) int { return op(x, y) } func multiplyNumbers(x int, y int) int { return x * y } func main() { result := calculation (2, 5, multiplyNumbers) fmt.Println("Result:", result) }
When the above code is compiled and executed, it produces the following result −
Result: 10
Returning a Function as a Value
A function can also return a function as a value in the Go language. It is useful when you want to return an expression calculated by creating any function.
Example
package main import "fmt" // Returning a Function as a Value func calculation(factor int) func(int) int { return func(value int) int { return factor * value } } func main() { multiplyByTwo := calculation(2) result := multiplyByTwo(20) fmt.Println("Result:", result) }
When the above code is compiled and executed, it produces the following result −
Result: 40