zoukankan      html  css  js  c++  java
  • Go入门笔记34-Go 使用Ioctl

    Go获取控制台宽度
    1、代码

    package main
    
    import (
    	"fmt"
    	"runtime"
    	"syscall"
    	"unsafe"
    )
    
    const (
    	TIOCGWINSZ     = 0x5413
    	TIOCGWINSZ_OSX = 1074295912
    )
    
    type window struct {
    	Row    uint16
    	Col    uint16
    	Xpixel uint16
    	Ypixel uint16
    }
    
    func terminalWidth() (int, error) {
    	w := new(window)
    	tio := syscall.TIOCGWINSZ
    	if runtime.GOOS == "darwin" {
    		tio = TIOCGWINSZ_OSX
    	}
    	res, _, err := syscall.Syscall(syscall.SYS_IOCTL,
    		uintptr(syscall.Stdin),
    		uintptr(tio),
    		uintptr(unsafe.Pointer(w)),
    	)
    	if int(res) == -1 {
    		return 0, err
    	}
    	return int(w.Col), nil
    }
    
    func main() {
    	width, _ := terminalWidth()
    	fmt.Println(width)
    }
    

    2、运行结果
    image

    本博客是个人工作中记录,遇到问题可以互相探讨,没有遇到的问题可能没有时间去特意研究,勿扰。
    另外建了几个QQ技术群:
    2、全栈技术群:616945527,加群口令abc123
    2、硬件嵌入式开发: 75764412
    3、Go语言交流群:9924600

    闲置域名www.nsxz.com出售(等宽等高字符四字域名)。
  • 相关阅读:
    洛谷
    洛谷
    洛谷
    51nod
    洛谷
    洛谷
    51nod
    洛谷
    2019五一训练记录
    2019.5.4备战省赛组队训练赛第十九场
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/15207634.html
Copyright © 2011-2022 走看看