Generate Random Colors in Go - Hex, RGB & color.RGBA
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Go
What: Generate random colors in Go using hex strings, RGB values, and color.RGBA objects with math/rand package for web and graphics applications.
Try it: Use our interactive Colors generator or integrate this code into your Go application.
Generate random colors in Go using hex strings, RGB values, and color.RGBA objects with math/rand package for web and graphics applications. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Ruby or use our interactive web generator.
Go Code Example
package main
import (
"fmt"
"image/color"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
// Generate Random Hex Color
fmt.Println("Hex Color:", randomHexColor())
// Generate Random RGB Color
fmt.Println("RGB Color:", randomRgbColor())
// Generate Random color.RGBA
rgba := randomColorRGBA()
fmt.Printf("RGBA: color.RGBA{R:%d, G:%d, B:%d, A:255}\n",
rgba.R, rgba.G, rgba.B)
// Generate Random HSL Color
fmt.Println("HSL Color:", randomHslColor())
// Generate Pastel Color
fmt.Println("Pastel Color:", randomPastelColor())
// Generate Color Palette
palette := generateColorPalette(5)
fmt.Println("Color Palette:", palette)
}
func randomHexColor() string {
return fmt.Sprintf("#%06x", rand.Intn(0x1000000))
}
func randomRgbColor() string {
r := rand.Intn(256)
g := rand.Intn(256)
b := rand.Intn(256)
return fmt.Sprintf("rgb(%d, %d, %d)", r, g, b)
}
func randomColorRGBA() color.RGBA {
return color.RGBA{
R: uint8(rand.Intn(256)),
G: uint8(rand.Intn(256)),
B: uint8(rand.Intn(256)),
A: 255,
}
}
func randomHslColor() string {
h := rand.Intn(361)
s := rand.Intn(101)
l := rand.Intn(101)
return fmt.Sprintf("hsl(%d, %d%%, %d%%)", h, s, l)
}
func randomPastelColor() string {
h := rand.Intn(361)
s := rand.Intn(76) + 25 // 25-100
l := rand.Intn(21) + 70 // 70-90
return fmt.Sprintf("hsl(%d, %d%%, %d%%)", h, s, l)
}
func generateColorPalette(count int) []string {
palette := make([]string, count)
for i := 0; i < count; i++ {
palette[i] = randomHexColor()
}
return palette
}
func rgbToHex(r, g, b uint8) string {
return fmt.Sprintf("#%02x%02x%02x", r, g, b)
}
[EXPLANATION]
Go uses math/rand package for random number generation. Initialize with rand.Seed(time.Now().UnixNano()) for different sequences. For hex colors, use fmt.Sprintf("#%06x", rand.Intn(0x1000000)) to format 6-digit hex. RGB uses rand.Intn(256) for values 0-255. The color.RGBA struct from image/color uses uint8 for each channel. HSL is formatted as strings. Use make([]string, count) to preallocate slices for color palettes.
Expected Output
Hex Color: #7b3f9e
RGB Color: rgb(189, 78, 142)
RGBA: color.RGBA{R:207, G:142, B:89, A:255}
HSL Color: hsl(287, 67%, 54%)
Pastel Color: hsl(156, 78%, 82%)
Color Palette: [#2d5a8f #e85d75 #ffa600 #58b368 #bc5090]
Common Use Cases
- Generate colors for web servers and APIs
- Create color palettes for image processing
- Assign colors to data visualization charts
- Generate random colors for SVG graphics
- Create color schemes for terminal applications
Important Notes
-
rand.Seed()should be called once at program start -
rand.Intn(n)returns values 0 to n-1 -
color.RGBAuses uint8 (0-255) for each channel -
For concurrent usage, use
rand.New(rand.NewSource(seed)) -
%02xformats with 2-digit hex and leading zeros
Try Our Interactive Generator
Don't want to write code? Use our free web-based Colors generator with instant results.
TRY COLORS GENERATOR →Other Programming Languages
View Colors generation code examples in PHP
View Colors generation code examples in JavaScript
View Colors generation code examples in Python
View Colors generation code examples in Java
View Colors generation code examples in C#
View Colors generation code examples in C++
View Colors generation code examples in Ruby