gpt4 book ai didi

go - Go 的分页输出

转载 作者:IT王子 更新时间:2023-10-29 02:27:57 33 4
gpt4 key购买 nike

我正在尝试使用 $PAGER 或手动调用 moreless 从 golang 打印到 stdout 以允许用户轻松滚动很多选择。我怎样才能做到这一点?

最佳答案

您可以使用 os/exec包启动一个运行 less(或 $PAGER 中的任何内容)的进程,然后将字符串通过管道传输到其标准输入。以下对我有用:

func main() {
// Could read $PAGER rather than hardcoding the path.
cmd := exec.Command("/usr/bin/less")

// Feed it with the string you want to display.
cmd.Stdin = strings.NewReader("The text you want to show.")

// This is crucial - otherwise it will write to a null device.
cmd.Stdout = os.Stdout

// Fork off a process and wait for it to terminate.
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}

关于go - Go 的分页输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705716/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com