func removeService() error {
	serviceManager, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer serviceManager.Disconnect()
	service, err := serviceManager.OpenService(svcName)
	if err != nil {
		return fmt.Errorf("service %s is not installed", svcName)
	}
	defer service.Close()
	return service.Delete()
}
removeService attempts to uninstall the soterd service.  Typically this should
be done by the msi uninstaller, but it is provided here since it can be
useful for development.  Not the eventlog entry is intentionally not removed
since it would invalidate any existing event log messages.
package main
import (
	"fmt"
	"os"
	"path/filepath"
	"time"
	"github.com/btcsuite/winsvc/eventlog"
	"github.com/btcsuite/winsvc/mgr"
	"github.com/btcsuite/winsvc/svc"
)
const (
	svcName = "soterdsvc"
	svcDisplayName = "Soterd Service"
	svcDesc = "Downloads and stays synchronized with the soter block " +
		"dag and provides dag services to applications."
)
var elog *eventlog.Log
func logServiceStartOfDay(srvr *server) {
	var message string
	message += fmt.Sprintf("Version %s\n", version())
	message += fmt.Sprintf("Configuration directory: %s\n", defaultHomeDir)
	message += fmt.Sprintf("Configuration file: %s\n", cfg.ConfigFile)
	message += fmt.Sprintf("Data directory: %s\n", cfg.DataDir)
	elog.Info(1, message)
}
type soterdService struct{}
func (s *soterdService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
	const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
	changes <- svc.Status{State: svc.StartPending}
	doneChan := make(chan error)
	serverChan := make(chan *server)
	go func() {
		err := soterdMain(serverChan)
		doneChan <- err
	}()
	changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
	var mainServer *server
loop:
	for {
		select {
		case c := <-r:
			switch c.Cmd {
			case svc.Interrogate:
				changes <- c.CurrentStatus
			case svc.Stop, svc.Shutdown:
				changes <- svc.Status{State: svc.StopPending}
				shutdownRequestChannel <- struct{}{}
			default:
				elog.Error(1, fmt.Sprintf("Unexpected control "+
					"request #%d.", c))
			}
		case srvr := <-serverChan:
			mainServer = srvr
			logServiceStartOfDay(mainServer)
		case err := <-doneChan:
			if err != nil {
				elog.Error(1, err.Error())
			}
			break loop
		}
	}
	changes <- svc.Status{State: svc.Stopped}
	return false, 0
}
func installService() error {
	exePath, err := filepath.Abs(os.Args[0])
	if err != nil {
		return err
	}
	if filepath.Ext(exePath) == "" {
		exePath += ".exe"
	}
	serviceManager, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer serviceManager.Disconnect()
	service, err := serviceManager.OpenService(svcName)
	if err == nil {
		service.Close()
		return fmt.Errorf("service %s already exists", svcName)
	}
	service, err = serviceManager.CreateService(svcName, exePath, mgr.Config{
		DisplayName: svcDisplayName,
		Description: svcDesc,
	})
	if err != nil {
		return err
	}
	defer service.Close()
	eventlog.Remove(svcName)
	eventsSupported := uint32(eventlog.Error | eventlog.Warning | eventlog.Info)
	return eventlog.InstallAsEventCreate(svcName, eventsSupported)
}