Closure not returning the desired output












-1















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, "n")

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.










share|improve this question




















  • 1





    Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

    – Adrian
    Nov 15 '18 at 18:04
















-1















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, "n")

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.










share|improve this question




















  • 1





    Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

    – Adrian
    Nov 15 '18 at 18:04














-1












-1








-1


0






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, "n")

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.










share|improve this question
















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, "n")

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.







function go closures






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:23









Flimzy

39.5k116699




39.5k116699










asked Nov 15 '18 at 17:43









sedawkgrepsedawkgrep

1051213




1051213








  • 1





    Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

    – Adrian
    Nov 15 '18 at 18:04














  • 1





    Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

    – Adrian
    Nov 15 '18 at 18:04








1




1





Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

– Adrian
Nov 15 '18 at 18:04





Your function returns a function. You're printing the function (what you see is its address) instead of calling the function and printing its return value.

– Adrian
Nov 15 '18 at 18:04












1 Answer
1






active

oldest

votes


















2














You have to invoke the function:



func main() {
fmt.Println(getLine(base64Decode())())
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325161%2fclosure-not-returning-the-desired-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You have to invoke the function:



    func main() {
    fmt.Println(getLine(base64Decode())())
    }





    share|improve this answer




























      2














      You have to invoke the function:



      func main() {
      fmt.Println(getLine(base64Decode())())
      }





      share|improve this answer


























        2












        2








        2







        You have to invoke the function:



        func main() {
        fmt.Println(getLine(base64Decode())())
        }





        share|improve this answer













        You have to invoke the function:



        func main() {
        fmt.Println(getLine(base64Decode())())
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:49









        nightfury1204nightfury1204

        1,769410




        1,769410
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325161%2fclosure-not-returning-the-desired-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine