首頁  >  文章  >  後端開發  >  go語言如何改變回顯顏色

go語言如何改變回顯顏色

藏色散人
藏色散人原創
2020-12-16 15:24:413073瀏覽

go語言改變回顯顏色的方法:先開啟對應的go檔;然後透過「FontColor Color=Color{}」方法給字型顏色物件賦值;最後透過「func ColorPrint(s string, i int ){}”方法輸出有顏色的字型即可。

go語言如何改變回顯顏色

本文環境:Windows7系統、Go1.11.2版,本文適用於所有品牌的電腦。

推薦:《golang教學

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

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

本方法只限用於Windows系統

應用程式場景

需要輸出大量資訊的運作日誌(一般是伺服器,Windows系統的)

某類客戶端的偵錯介面(一般是遊戲,特別是有第三方模組的)

程式碼範例

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語言如何改變回顯顏色的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn