Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

color

> 编程语言
Open source

Color package for Go (golang)

8.0K stars0 likes3 views
WebsiteGitHub

About

Color package for Go (golang)

color

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Install

go get github.com/fatih/color

Examples

Standard colors

go
// Print with default helper functions
color.Cyan("Prints text in cyan.")

// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")

// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")

RGB colors

If your terminal supports 24-bit colors, you can use RGB color codes.

go
color.RGB(255, 128, 0).Println("foreground orange")
color.RGB(230, 42, 42).Println("foreground red")

color.BgRGB(255, 128, 0).Println("background orange")
color.BgRGB(230, 42, 42).Println("background red")

Mix and reuse colors

…

Use your own output (io.Writer)

go
// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")

blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")

Custom print functions (PrintFunc)

go
// Create a custom print function for convenience
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)

// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")

Custom fprint functions (FprintFunc)

go
blue := color.New(color.FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)

// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, "Don't forget this...")

Insert into noncolor strings (SprintFunc)

…

Plug into existing code

go
// Use handy standard colors
color.Set(color.FgYellow)

fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")

color.Unset() // Don't forget to unset

// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your function

fmt.Println("All text will now be bold magenta.")

Disable/Enable color

There might be a case where you want to explicitly disable/enable color output. the go-isatty package will automatically disable color output for non-tty output streams (for example if the output were piped directly to less).

The color package also disables color output if the NO_COLOR environment variable is set to a non-empty string.

Color has support to disable/enable colors programmatically both globally and for single color definitions. For example suppose you have a CLI app and a -no-color bool flag. You can easily disable the color output with:

go
var flagNoColor = flag.Bool("no-color", false, "Disable color output")

if *flagNoColor {
	color.NoColor = true // disables colorized output
}

It also has support for single color definitions (local). You can disable/enable color output on the fly:

go
c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

GitHub Actions

To output color in GitHub Actions (or other CI systems that support ANSI colors), make sure to set color.NoColor = false so that it bypasses the check for non-tty output streams.

Credits

  • Fatih Arslan
  • Windows support via @mattn: colorable

License

The MIT License (MIT) - see LICENSE.md for more details

Issues· 34 open

View all issuesOpen on GitHub
  • #271

    Feature request: .RemoveColor() function

    Updated Mar 1, 2026
  • #176

    [bug] yellow color does not take effect in powershell

    Updated Dec 15, 2025
  • #263

    When starting debugging with the F5 key in vscode, the output has no color. Output it with the following code and there will be no problem!

    Updated Aug 18, 2025
  • #257

    Color (wrongly) disabled in VSCode test results output

    Updated Apr 14, 2025
  • #253

    Support hyperlinking escape chars

    Updated Mar 27, 2025
  • #240

    Library not compatible with text/tabwriter

    Updated Feb 10, 2025
  • #229

    Docker container console support

    Updated Dec 25, 2024
  • #241

    Print string ended by symbol new line '\n'

    Updated Oct 3, 2024
  • #230

    Set/UnsetWriter asymmetry

    Updated Oct 3, 2024
  • #239

    CrossedOut not working over SSH

    Updated Sep 16, 2024

> Tags

Goansicolorcoloringgo

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言