How to host a Go bot or service for your community
Run a Go bot, API or worker on a panel: go.mod, building for Linux, environment secrets, graceful shutdown, structured logs, memory limits and updates.
Go is a comfortable language for the small services a community runs: a chat bot, a status checker for your game servers, a little API behind your website, a relay that forwards webhooks into a channel. It compiles to one file, starts instantly and usually stays lean. Hosting it is simpler than hosting most interpreted projects, but there are still a few things to get right: building the right binary, keeping secrets out of it, shutting down cleanly and staying inside your memory limit.
This guide covers running a Go program for your own community or project on a hosting panel. If you are thinking of selling Go hosting to others, read how to start a Go app hosting business.
The shape of a Go project
A typical small service:
statusbot/
go.mod
go.sum
main.go
internal/
checks/
checks.go
chat/
chat.go
.env.example
go.mod declares the module path, the Go version and the dependencies; go.sum records checksums for them. Commit both. Run go mod tidy before you build to drop unused dependencies and add missing ones, so the module files match the code.
module example.com/statusbot
go 1.22
require github.com/example/chatlib v1.4.0
The internal/ folder is a Go convention: packages inside it can only be imported by code in the same module. It keeps helpers private and the layout readable.
Build for the server, not for your laptop
The server runs Linux on a specific CPU architecture. Your laptop may run macOS or Windows, possibly on ARM. Go cross-compiles with two environment variables:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o statusbot .
Why each part matters:
GOOS=linuxbuilds for Linux. A macOS or Windows binary will not run.GOARCHmust match the server's CPU. A mismatch gives "exec format error". Check what your server uses;amd64is the most common for hosting.CGO_ENABLED=0produces a fully static binary with no dependency on the server's C library. If your program needs cgo (some SQLite drivers do), either switch to a pure Go driver or build on the server instead.
Upload the binary, make sure it is executable, and set the start command to run it, for example ./statusbot. If you prefer to build on the server, upload the source with go.mod and go.sum and run go build there before starting; it needs more disk for the module cache and a Go toolchain at least as new as the version in go.mod. Downloads and release notes are at go.dev.
Configuration and secrets
Do not compile secrets into the binary. Anything embedded in a Go binary can be read back out of the file with basic tools. Read tokens and keys from environment variables at startup and fail clearly if they are missing:
func mustEnv(name string) string {
v := os.Getenv(name)
if v == "" {
log.Fatalf("missing environment variable %s", name)
}
return v
}
token := mustEnv("CHAT_TOKEN")
Go has no built-in .env loader, so either set the variables through your panel's startup settings if it has them, or use a small library such as godotenv to read a .env file you upload through the file manager. Keep .env out of version control and document the names in .env.example.
If a token leaks, revoke it on the service that issued it first, then update the server. Rebuilding does not help if the old token still works.
Listening on the right address and port
Bots and workers that only make outgoing connections need no port. An API or webhook receiver must listen on the port the panel allocated and on all interfaces:
port := os.Getenv("SERVER_PORT")
if port == "" {
port = "8080"
}
srv := &http.Server{Addr: "0.0.0.0:" + port, Handler: mux}
Check which variable your panel provides for the allocated port, or set your own to the number in its network section. Listening on 127.0.0.1 or a hard-coded port is the usual reason a hosted Go service runs but cannot be reached.
Graceful shutdown
When you stop or restart a server, the process gets a termination signal and, if it does not exit in time, is killed. Go makes clean shutdown easy with signal.NotifyContext:
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
srv.Shutdown(shutdownCtx)
Pass the same ctx to your background workers so they stop their loops when it is cancelled. Close databases last. This prevents half-written files and lost state on every restart.
Crash loops
A Go program in a crash loop usually fails in the first second. Typical causes on a panel:
log.Fatalon a missing environment variable (good: the message tells you which one).- A binary for the wrong architecture, or not executable.
- A config or data file at a relative path that does not exist in the server's working directory.
- A panic during startup from a nil value, often a config field that was never set.
Stop the server, read the first lines after startup and fix that cause. For panics during normal running, recover inside goroutines that handle requests or jobs, log the error and keep going, so one bad message does not take the whole service down. The standard net/http server already recovers panics in handlers; your own goroutines need their own protection.
Logs
Write logs to standard output so they appear in the panel console. Since Go 1.21, the standard library includes log/slog for structured logs with levels:
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger.Info("check finished", "server", name, "online", ok)
Key-value logs are easy to scan in a console and easy to search later. Log what happened and to which resource; never log tokens or private messages.
Memory limits
Go manages memory with a garbage collector, and by default it does not know about your container's memory limit. Since Go 1.19, you can give it a soft limit with the GOMEMLIMIT environment variable, for example a value somewhat below your plan's memory so the collector works harder before the container limit is reached. Treat the exact margin as a starting point and watch the memory graph.
If memory still climbs steadily, look for goroutine leaks (goroutines blocked forever on a channel nobody writes to), maps that only grow and response bodies that are never closed. The net/http/pprof package can show you where memory goes when you need to dig.
Updating
- Back up the server, including any data files.
- Build the new binary with the same
GOOS,GOARCHandCGO_ENABLEDsettings. - Stop the server, replace the binary, start it again.
- Watch the first log lines.
Keep the previous binary next to the new one under a different name. If the update misbehaves, rolling back is a rename and a restart. Update dependencies on purpose with go get and go mod tidy, test at home, then build.
Running it through a branded panel with Enzonic for Resellers
If you run services and game servers for a community, Enzonic for Resellers gives you a panel with your own name and colours for all of them. Opening a site is free, and panel-only mode serves only sign-in and the customer panel, with no storefront.
The panel includes a live console with power controls and CPU, memory and disk stats, a file manager and SFTP for uploading binaries, backups (also scheduled), scheduled tasks, subusers and network ports. With Enzonic for Resellers, Go apps runs on Enzonic nodes in Europe, North America and Asia and on your own Pterodactyl or Pelican Wings nodes, so you can sell it without running any hardware. You can also connect your own Pterodactyl Wings or Pelican Wings machine and run Go services there. Servers you grant on Enzonic nodes have their base price charged monthly from your credits.
Other languages in your stack? See the guides for Node.js, Python and Java. Open a site to try it.
Frequently asked questions
Do I need Go installed on the server?
No, if you upload a compiled Linux binary. You only need the Go toolchain on the server if you build there.
Why does my Go binary say exec format error?
It was built for the wrong operating system or CPU architecture. Rebuild with GOOS=linux and the GOARCH that matches the server.
Is it safe to embed my bot token in the binary?
No. Strings inside a binary can be extracted easily. Read secrets from environment variables at startup instead.
How do I stop my Go service from using too much memory?
Set GOMEMLIMIT a bit below the plan's memory, then look for leaks such as stuck goroutines and maps that only grow if memory keeps rising.