workers

package module
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 3 Imported by: 60

README

workers

Go Reference Discord Server

  • workers is a package to run an HTTP server written in Go on Cloudflare Workers.
  • This package can easily serve http.Handler on Cloudflare Workers.
  • Caution: This is an experimental project.

Features

  • serve http.Handler
  • R2
    • Head
    • Get
    • Put
    • Delete
    • List
    • Options for R2 methods
  • KV
    • Get
    • List
    • Put
    • Delete
    • Options for KV methods
  • Cache API
  • Durable Objects
    • Calling stubs
  • D1 (alpha)
  • Environment variables
  • FetchEvent
  • Cron Triggers
  • TCP Sockets
  • Queues
    • Producer
    • Consumer

Installation

go get github.com/syumai/workers

Usage

implement your http.Handler and give it to workers.Serve().

func main() {
	var handler http.HandlerFunc = func (w http.ResponseWriter, req *http.Request) { ... }
	workers.Serve(handler)
}

or just call http.Handle and http.HandleFunc, then invoke workers.Serve() with nil.

func main() {
	http.HandleFunc("/hello", func (w http.ResponseWriter, req *http.Request) { ... })
	workers.Serve(nil) // if nil is given, http.DefaultServeMux is used.
}

For concrete examples, see _examples directory.

Quick Start

  • You can easily create and deploy a project from Deploy to Cloudflare button.

Deploy to Cloudflare

  • If you want to create a project manually, please follow the guide below.
Requirements
  • Node.js (and npm)
  • Go 1.24.0 or later
Create a new Worker project

Run the following command:

npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-go
Initialize the project
  1. Navigate to your new project directory:
cd my-app
  1. Initialize Go modules:
go mod init
go mod tidy
  1. Start the development server:
npm start
  1. Verify the worker is running:
curl http://localhost:8787/hello

You will see "Hello!" as the response.

If you want a more detailed description, please refer to the README.md file in the generated directory.

FAQ

How do I deploy a worker implemented in this package?

To deploy a Worker, the following steps are required.

  • Create a worker project using wrangler.
  • Build a Wasm binary.
  • Upload a Wasm binary with a JavaScript code to load and instantiate Wasm (for entry point).

The worker-go template contains all the required files, so I recommend using this template.

But Go (not TinyGo) with many dependencies may exceed the size limit of the Worker (3MB for free plan, 10MB for paid plan). In that case, you can use the TinyGo template instead.

Where can I have discussions about contributions, or ask questions about how to use the library?

You can do both through GitHub Issues. If you want to have a more casual conversation, please use the Discord server.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Done added in v0.27.0

func Done() <-chan struct{}

func Ready added in v0.27.0

func Ready()

func Serve

func Serve(handler http.Handler)

Server serves http.Handler as a normal HTTP server. if the given handler is nil, http.DefaultServeMux will be used. As a port number, PORT environment variable or default value (9900) is used. This function is implemented for non-JS environments for debugging purposes.

func ServeNonBlock added in v0.27.0

func ServeNonBlock(http.Handler)

Types

This section is empty.

Directories

Path Synopsis
_examples
browser module
cache module
env module
fetch module
fetch-event module
hello module
incoming module
kv-counter module
queues module
sockets module
_templates
d1
kv
r2
cmd
examples
exp
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL