gpt4 book ai didi

windows - 使用 golang 在 Windows 中获取终端宽度

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

是否可以在 Go 中获取终端宽度?

我尝试使用 http://github.com/nsf/termbox-go使用代码:

package main

import (
"fmt"

"github.com/nsf/termbox-go"
)

func main() {
fmt.Println(termbox.Size())
}

但它会打印 0 0

我也试过http://github.com/buger/goterm但是当我尝试 go get 时,出现错误:

$ go get github.com/buger/goterm
# github.com/buger/goterm
..\..\buger\goterm\terminal.go:78: undefined: syscall.SYS_IOCTL
..\..\buger\goterm\terminal.go:82: not enough arguments in call to syscall.Syscall

关于如何获取终端宽度还有其他想法吗?

最佳答案

在调用termbox.Size()之前需要调用termbox.Init(),然后在调用termbox.Close()时你完成了。

package main

import (
"fmt"

"github.com/nsf/termbox-go"
)

func main() {
if err := termbox.Init(); err != nil {
panic(err)
}
w, h := termbox.Size()
termbox.Close()
fmt.Println(w, h)
}

关于windows - 使用 golang 在 Windows 中获取终端宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418950/

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