Go Lang给应用添加带彩色的启动横幅

1.命令行安装相关依赖包:
- go get github.com/dimiro1/banner
- go get github.com/mattn/go-colorable

 

banner.go代码:

package main

import (
	"fmt"
	"github.com/dimiro1/banner"
	"github.com/mattn/go-colorable"
)

func init() {
	isEnabled := true
	isColorEnabled := true
	templ := `{{ .Title "Banner" "" 4 }}
   {{ .AnsiColor.BrightCyan }}The title will be ascii and indented 4 spaces{{ .AnsiColor.Default }}
   GoVersion: {{ .GoVersion }}
   GOOS: {{ .GOOS }}
   GOARCH: {{ .GOARCH }}
   NumCPU: {{ .NumCPU }}
   GOPATH: {{ .GOPATH }}
   GOROOT: {{ .GOROOT }}
   Compiler: {{ .Compiler }}
   ENV: {{ .Env "GOPATH" }}
   Now: {{ .Now "Monday, 2 Jan 2006" }}
   {{ .AnsiColor.BrightGreen }}This text will appear in Green
   {{ .AnsiColor.BrightRed }}This text will appear in Red{{ .AnsiColor.Default }}`
	banner.InitString(colorable.NewColorableStdout(), isEnabled, isColorEnabled, templ)
	fmt.Println("\nGo应用添加带彩色的启动横幅")
}

func main() {
	fmt.Println("Hi World!")
}

 

2.运行banner.go源码文件

 

posted @ 2024-08-03 17:28  沉雪's  阅读(44)  评论(0)    收藏  举报