亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關閉不返回所需的輸出

關閉不返回所需的輸出

Go
侃侃爾雅 2023-05-08 16:11:38
我想知道為什么我的函數不返回這些行。我正在使用閉包,我的目標是顯示解碼文本中的每一行。我能夠使用 Python 實現這一目標。這是我的 Python 代碼: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這是我的代碼:package mainimport (    "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()))}當我運行它時會發生什么,它只打印:0x1095850而不是This is the first line從文本中打印。
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

您必須調用該函數:

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


查看完整回答
反對 回復 2023-05-08
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號