Go Recursion

Learn via video courses
Topics Covered

Overview

Recursion refers to an event when a function calls itself directly or indirectly. When using recursion in coding every call's scope will be reduced and the result will converge at some point with the help of the base condition which is the final executable statement in recursion and stops any further function calls.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction

In Golang, Recursion is achieved using functions. We can have a function calling itself to create a recursive function.

Pseudo-code:

The above function will print the greetings infinitely as there is no base condition. A base condition is the criteria when the execution of recursion must stop.

In programming, recursion is used at the places where a large problem can be broken down into smaller subproblems, and the solutions of subproblems ultimately solve the main problem.

Classification of Recursion

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Direct and Indirect Recursion

Direct Recursion:

Direct recursion is the state of function in which it calls itself and does not need any assistance from any other function. this means the function will call itself directly. Here the base condition is n==1, once n = 1 then it will start returning, and the function call ends.

Output:

Example for Direct Recursion : playground link

Indirect Recursion:

Indirect recursion is the state of function in which it calls itself and needs assistance from other functions so that it will be executed. this means the function will call indirectly using other functions.

Output:

Example of Indirect recursion: playground link

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Finite and Infinite Recursion

Finite Recursion:

Finite recursion is the state of function in which it calls itself and stops its execution at a boundary or base condition.

Output:

Example for Finite recursion: playground link

Infinite Recursion:

Infinite recursion is the state of function in which it calls itself and its execution does not have a boundary condition. This makes the function run infinitely.

Output:

Example for Infinite recursion: playground link

Tail Recursion and Head Recursion

Tail Recursion:

Tail recursion is the state of a function in which it calls the function at the end of the recursive function. As the recurring call is the last thing in the function it is referred to as Tail recursion.

Output:

Example for Tail recursion: playground link

Head Recursion

Head recursion is the state of the function in which it calls the function at the start of the recursive function. As the recurring call is the first thing in the function it is referred to as Head recursion.

Output:

Example for Head recursion: playground link

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Recursion Using Anonymous Functions in Golang

In Golang, We can implement recursion using anonymous functions, Anonymous functions are those functions in golang that do not have a name.

Output:

Example for Recursion using anonymous functions in Golang: playground link

Example of Recursive Go Programs

Conclusion

  • Understood what is Golang recursion.
  • Explained classification of recursions.
  • Implemented examples in Golang for recursion.