func (e *Elector) initialize() error {
	exists, _, err := e.conn.Exists(e.basePath)
	if err != nil {
		return errors.Wrapf(err, "could not check if base path %s exists", e.basePath)
	}
	if exists {
		return nil
	}
	segments := strings.Split(e.basePath, "/")
	create := "/"
	for _, segment := range segments {
		create = path.Join(create, segment)
		exists, _, err := e.conn.Exists(create)
		if err != nil {
			return errors.Wrapf(err, "could not check path '%s'", create)
		}
		if exists {
			continue
		}
		_, err = e.conn.Create(create, []byte{}, 0, e.acl)
		if err != nil {
			return errors.Wrapf(err, "could not create path '%s'", create)
		}
	}
	return nil
}
initialize sets up the basePath if necessary
package elector
import (
	"fmt"
	"path"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"sync"
	"github.com/pkg/errors"
	"github.com/samuel/go-zookeeper/zk"
)
type IElector interface {
	LeaderIdent() string
	Events() <-chan Event
	Close() error
}
var _ IElector = &Elector{}
type Elector struct {
	acl      []zk.ACL
	conn     *zk.Conn
	events   chan Event
	ident    string        
	basePath string        
	closer   func() error  
	mut         sync.Mutex  
	leaderIdent string      
	isLeader    bool        
}
var (
	sequenceRe = regexp.MustCompile(`.*-lock-(-?\d+)$`)
)
func Start(ident string, basePath string, acl []zk.ACL, connector Connector) (*Elector, error) {
	if strings.TrimSpace(ident) == "" {
		return nil, errors.New("ident must not be blank")
	}
	if acl == nil {
		acl = zk.WorldACL(zk.PermAll)
	}
	conn, zkEvents, err := connector.Connect()
	if err != nil {
		return nil, err
	}
	elector := &Elector{
		acl:      acl,
		ident:    ident,
		conn:     conn,
		basePath: basePath,
		events:   make(chan Event),
		closer:   connector.Close,
	}
	if err := elector.initialize(); err != nil {
		return nil, errors.Wrap(err, "elector initialization failed")
	}
	go elector.start(zkEvents)
	return elector, nil
}
func (e *Elector) LeaderIdent() string {
	e.mut.Lock()
	defer e.mut.Unlock()
	return e.leaderIdent
}
func (e *Elector) Events() <-chan Event {
	return e.events
}
func (e *Elector) Close() error {
	return e.closer()
}