Golang : Get terminal width and height example




Problem :

You want to find out a terminal/console's width and height to calibrate your Golang text based program display. How to do that?

Solution :

Use the GoTerm package to determine the terminal dimension. For example :

 package main

 import (
 "fmt"
 "github.com/buger/goterm"
 )

 func main() {
 terminalHeight := goterm.Height()
 terminalWidth := goterm.Width()

 fmt.Println("Terminal height : ", terminalHeight)
 fmt.Println("Terminal width : ", terminalWidth)

 }

Sample output :

Terminal height : 50

Terminal width : 139

References :

https://godoc.org/github.com/buger/goterm





By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement