golang控制台颜色输出(for windows)

Go语言:控制台输出有颜色的字

https://www.jianshu.com/p/24b1bec99d76

本方法只限用于 Windows系统

应用场景


  1. 需要输出大量信息的运行日志(一般是服务器,Windows系统的)
  2. 某类客户端的调试界面(一般是游戏,特别是有第三方模组的)

代码示例


package main

import (
    "syscall"
)

var (
    kernel32    *syscall.LazyDLL  = syscall.NewLazyDLL(`kernel32.dll`)
    proc        *syscall.LazyProc = kernel32.NewProc(`SetConsoleTextAttribute`)
    CloseHandle *syscall.LazyProc = kernel32.NewProc(`CloseHandle`)

    // 给字体颜色对象赋值
    FontColor Color = Color{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
)

type Color struct {
    black        int // 黑色
    blue         int // 蓝色
    green        int // 绿色
    cyan         int // 青色
    red          int // 红色
    purple       int // 紫色
    yellow       int // 黄色
    light_gray   int // 淡灰色(系统默认值)
    gray         int // 灰色
    light_blue   int // 亮蓝色
    light_green  int // 亮绿色
    light_cyan   int // 亮青色
    light_red    int // 亮红色
    light_purple int // 亮紫色
    light_yellow int // 亮黄色
    white        int // 白色
}

// 输出有颜色的字体
func ColorPrint(s string, i int) {
    handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(i))
    print(s)
    CloseHandle.Call(handle)
}

func main() {
    ColorPrint(`红色`, FontColor.red)
    ColorPrint(`蓝色`, FontColor.blue)
    ColorPrint(`白色`, FontColor.white)
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值