I am wondering why my function does not return the lines. I'm using closures and my goal is to display each line from a decoded text. I was able to achieve this using Python.
Here's my Python code:
def get_line():
lines = base64_decode()
index = 0
def closure():
nonlocal index
def go_next():
nonlocal index
next_line = line[index]
index += 1
return next_line
if index != len(lines):
return go_next()
else:
index = 0
return go_next()
return closure
Here's my Go code:
package main
import (
"encoding/base64"
"fmt"
"log"
"strings"
)
func base64Decode() string {
str := "REDACTED"
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.Fatal("Error:", err)
}
return string(data)
}
func getLine(str string) func() string {
i := 0
lines := strings.Split(str, "
")
return func() string {
if i != len(lines) {
nextLine := lines[i]
i++
return nextLine
}
return ""
}
}
func main() {
fmt.Println(getLine(base64Decode()))
}
What happens when I run this is it only prints: 0x1095850
instead of This is the first line
from the text.