dsio68964998 2018-11-15 17:43
浏览 35
已采纳

关闭不返回所需的输出

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.

  • 写回答

1条回答 默认 最新

  • donglu0494 2018-11-15 17:49
    关注

    You have to invoke the function:

    func main() {
        fmt.Println(getLine(base64Decode())())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分