溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

go語言怎樣改變回顯顏色

發(fā)布時間:2020-12-17 09:40:18 來源:億速云 閱讀:229 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)go語言怎樣改變回顯顏色的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

go語言改變回顯顏色的方法:首先打開相應的go文件;然后通過“FontColor Color=Color{}”方法給字體顏色對象賦值;最后通過“func ColorPrint(s string, i int){}”方法輸出有顏色的字體即可。

golang控制臺顏色輸出(for windows)

Go語言:控制臺輸出有顏色的字

本方法只限用于 Windows系統(tǒng)

應用場景

需要輸出大量信息的運行日志(一般是服務器,Windows系統(tǒng)的)

某類客戶端的調(diào)試界面(一般是游戲,特別是有第三方模組的)

代碼示例

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 // 淡灰色(系統(tǒng)默認值)
    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)
}

感謝各位的閱讀!關(guān)于go語言怎樣改變回顯顏色就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI