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

kol*_*rie 5 windows terminal go

可以在Go中获得端子宽度吗?

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

package main

import (
    "fmt"

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

func main() {
    fmt.Println(termbox.Size())
}
Run Code Online (Sandbox Code Playgroud)

但它打印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
Run Code Online (Sandbox Code Playgroud)

关于如何获得端子宽度的其他想法?

Ain*_*r-G 5

您需要termbox.Init()先致电termbox.Size(),然后再致电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)
}
Run Code Online (Sandbox Code Playgroud)