xmpp/id.go
author Chris Jones <chris@cjones.org>
Sat, 09 Nov 2013 12:09:37 -0700
changeset 178 ccfebbd9f49b
parent 126 367e76b3028e
permissions -rw-r--r--
Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.

package xmpp

// Code to generate unique IDs for outgoing messages.

import (
	"fmt"
)

var id <-chan string

func init() {
	// Start the unique id generator.
	idCh := make(chan string)
	id = idCh
	go func(ch chan<- string) {
		id := int64(1)
		for {
			str := fmt.Sprintf("id_%d", id)
			ch <- str
			id++
		}
	}(idCh)
}

// This function may be used as a convenient way to generate a unique
// id for an outgoing iq, message, or presence stanza.
func NextId() string {
	return <-id
}