Skip to content

Instantly share code, notes, and snippets.

@Adrian-Samuel
Last active April 23, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adrian-Samuel/dba90330a1b75d731df0a3cbde6dec93 to your computer and use it in GitHub Desktop.
Save Adrian-Samuel/dba90330a1b75d731df0a3cbde6dec93 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
//Option 1
func recursiveLoop(list []int, currentIndex int) {
if !(currentIndex == len(list)) {
fmt.Println(list[currentIndex])
recursiveLoop(list, currentIndex+1)
return
}
fmt.Println("Loop Finished!")
}
//Option 2
func recursiveLoopDirector(list []int, reverse bool) {
if len(list) != 0 {
if reverse {
fmt.Println(list[len(list)-1 : len(list)][0])
recursiveLoop(list[0:len(list)-1], reverse)
return
}
fmt.Println(list[0:1][0])
recursiveLoop(list[1:len(list)], reverse)
return
}
fmt.Println("Loop finished")
}
func main() {
list := []int{1, 2, 3, 4}
recursiveLoop(list) // result: 1,2,3,4
recursiveLoopDirector(list, true) // result: 4,3,2,1
recursiveLoopDirector(list, false) // result: 1,2,3,4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment