2023-04-30 11:54:32 +09:00
# worker-template-go
- A template for starting a Cloudflare Worker project with Go.
- This template uses [`workers` ](https://github.com/syumai/workers ) package to run an HTTP server.
## Notice
2025-02-23 23:54:29 +09:00
- 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 ](https://github.com/syumai/workers/tree/main/_templates/cloudflare/worker-tinygo ) instead.
2023-04-30 11:54:32 +09:00
## Usage
- `main.go` includes simple HTTP server implementation. Feel free to edit this code and implement your own HTTP server.
## Requirements
- Node.js
2025-02-23 23:54:29 +09:00
- Go 1.24.0 or later
2023-04-30 11:54:32 +09:00
2023-09-09 22:17:13 +09:00
## Getting Started
2025-02-23 23:54:29 +09:00
- Create a new worker project using this template.
2023-09-09 22:17:13 +09:00
```console
2025-02-23 23:54:29 +09:00
npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-go
2023-09-09 22:17:13 +09:00
```
2025-02-23 23:54:29 +09:00
- Initialize a project.
2023-09-09 22:17:13 +09:00
2023-04-30 12:18:47 +09:00
```console
cd my-app
2025-02-23 23:54:29 +09:00
go mod init
2023-04-30 12:18:47 +09:00
go mod tidy
2025-02-23 23:54:29 +09:00
npm start # start running dev server
2023-04-30 12:18:47 +09:00
curl http://localhost:8787/hello # outputs "Hello!"
2023-04-30 11:54:32 +09:00
```
## Development
### Commands
```
2025-02-23 23:54:29 +09:00
npm start # run dev server
# or
go run . # run dev server without Wrangler (Cloudflare-related features are not available)
npm run build # build Go Wasm binary
npm run deploy # deploy worker
2023-04-30 11:54:32 +09:00
```
### Testing dev server
- Just send HTTP request using some tools like curl.
```
$ curl http://localhost:8787/hello
Hello!
```
```
$ curl -X POST -d "test message" http://localhost:8787/echo
test message
```