Go

Implementing Gravatars with Go is relatively straightforward. Below you’ll find an example of how to do so.

Example Implementation

package gravatar

import (
	"crypto/sha256"
	"encoding/hex"
	"net/url"
	"strconv"
	"strings"
)

const (
	defaultScheme   = "https"
	defaultHostname = "www.gravatar.com"
)

func NewGravatarFromEmail(email string) Gravatar {
	hasher := sha256.Sum256([]byte(strings.TrimSpace(email)))
	hash := hex.EncodeToString(hasher[:])

	g := NewGravatar()
	g.Hash = hash
	return g
}

func NewGravatar() Gravatar {
	return Gravatar{
		Scheme: defaultScheme,
		Host:   defaultHostname,
	}
}

type Gravatar struct {
	Scheme  string
	Host    string
	Hash    string
	Default string
	Rating  string
	Size    int
}

func (g Gravatar) GetURL() string {
	path := "/avatar/" + g.Hash

	v := url.Values{}
	if g.Size > 0 {
		v.Add("s", strconv.Itoa(g.Size))
	}

	if g.Rating != "" {
		v.Add("r", g.Rating)
	}

	if g.Default != "" {
		v.Add("d", g.Default)
	}

	url := url.URL{
		Scheme:   g.Scheme,
		Host:     g.Host,
		Path:     path,
		RawQuery: v.Encode(),
	}

	return url.String()
}

See Go Gravatar for more information.


Provide a link to gravatar.com/profile and let your users know how to edit their Gravatar profile. Use of our free APIs is governed by these Guidelines for Responsible Use. Documentation is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


Last updated on:


Blog at WordPress.com.