go语言如何改变回显颜色


本文摘自php中文网,作者藏色散人,侵删。

go语言改变回显颜色的方法:首先打开相应的go文件;然后通过“FontColor Color=Color{}”方法给字体颜色对象赋值;最后通过“func ColorPrint(s string, i int){}”方法输出有颜色的字体即可。

本文环境:Windows7系统、Go1.11.2版,本文适用于所有品牌的电脑。

推荐:《golang教程

golang控制台颜色输出(for windows

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

本方法只限用于 Windows系统

应用场景

需要输出大量信息的运行日志(一般是服务器,Windows系统的)

某类客户端的调试界面(一般是游戏,特别是有第三方模组的)

代码示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

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)

}

以上就是go语言如何改变回显颜色的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Go语言package是什么

Go语言如何导入包

Go语言定义一个数组的方法有哪些

第一个 go 程序

Go语言如何删除切片

Go语言转义字符有哪些

Go语言和java语言的区别是什么?

Go语言通过命令方式生成可执行文件

Go语言能干什么

Go语言依赖管理介绍

更多相关阅读请进入《Go语言》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...