>  기사  >  백엔드 개발  >  Go 언어에서 에코 색상을 변경하는 방법

Go 언어에서 에코 색상을 변경하는 방법

藏色散人
藏色散人원래의
2020-12-16 15:24:413073검색

go 언어에서 에코 색상을 변경하는 방법: 먼저 해당 go 파일을 연 다음 "FontColor Color=Color{}" 메서드를 통해 글꼴 색상 개체에 값을 할당합니다. 마지막으로 "func ColorPrint(s string, i)를 사용합니다. int){}" 이 메서드는 컬러 글꼴을 출력합니다.

Go 언어에서 에코 색상을 변경하는 방법

이 글의 환경: Windows 7 시스템, Go1.11.2 버전, 이 글은 모든 브랜드의 컴퓨터에 적용됩니다.

추천: "golang 튜토리얼"

golang 콘솔 색상 출력(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으로 문의하세요.