我想知道為什么我的函數不返回這些行。我正在使用閉包,我的目標是顯示解碼文本中的每一行。我能夠使用 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 回答
- 0 關注
- 107 瀏覽
添加回答
舉報
0/150
提交
取消