DEV Community

Muhammad Saim
Muhammad Saim

Posted on

1

GoAvatar: Generate Unique Identicons in Go with Ease

By Muhammad Saim

User avatars help create a recognizable identity in web apps, social platforms, and online communities. Instead of relying on uploaded images, identicons (automatically generated avatars based on input data) offer a unique, visually distinct way to represent users.

🚀 GoAvatar is an open-source Go package I created to generate unique, symmetric identicons from any input string (such as usernames or email addresses).

Identicons are perfect for applications that need quick, automatically generated profile images while maintaining uniqueness and consistency.

✨ Features

Deterministic Identicons – The same input always generates the same avatar.

Symmetric Designs – The generated icons are mirrored for a clean, balanced look.

Customizable Sizes – Easily adjust the avatar size to fit your app's UI.

Fast & Lightweight – Minimal dependencies for quick generation.

📦 Installation

To install GoAvatar in your project, run:

go get github.com/MuhammadSaim/goavatar
Enter fullscreen mode Exit fullscreen mode

Then, import the package:

import "github.com/MuhammadSaim/goavatar"
Enter fullscreen mode Exit fullscreen mode

🚀 Usage Example
Generating an identicon and saving it as an image is simple:

package main

import (
    "github.com/MuhammadSaim/goavatar"
    "image/png"
    "os"
)

func main() {

    // Generate the avatar with a custom foreground and background color
    options := goavatar.Options{
        Width:   128, // Set custom image width (default is 256)
        Height:  128, // Set custom image height (default is 256)
        BgColor: color.RGBA{170, 120, 10, 255},  // Change background color (default is light gray)
        FgColor: color.RGBA{255, 255, 255, 255}, // Change foreground color (default is extracted from hash)
    }
    avatar := goavatar.Make("EchoFrost7", options)
    // Generates an avatar with a brownish background and white foreground, saving it as avatar.png


    file, err := os.Create("avatar.png")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    png.Encode(file, avatar)
}
Enter fullscreen mode Exit fullscreen mode

🎨 The above code generates an identicon for "exampleUsername" at 128x128 pixels and saves it as "avatar.png".

🔍 Sample Identicons
Here are a few identicons generated with GoAvatar:

  • "QuantumNomad42"

  • "EchoFrost7"

  • "NebulaTide19"

Here are some unique identicons generated using GoAvatar:

QuantumNomad42 EchoFrost7 NebulaTide19
QuantumNomad42 EchoFrost7 NebulaTide19

Every identicon is unique to the input string, making them perfect for user profiles, forums, and more!

🔗 Get Involved
GoAvatar is open-source! Contributions, issues, and feedback are welcome. If you find it useful, give it a ⭐ on GitHub and share it with fellow developers.

🔗 GitHub Repo: github.com/MuhammadSaim/goavatar

Happy coding! 🚀

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay