|
| 1 | +# Building Go Functions for Serverledge |
| 2 | + |
| 3 | +To ensure your Go function runs correctly on Serverledge's lightweight Alpine containers (avoiding `glibc` vs `musl` issues) and supports multi-architecture clusters (x86 & ARM), follow these build steps. |
| 4 | + |
| 5 | +## 1. Static Compilation |
| 6 | +You must disable CGO to make the executable self-contained and compatible with any Linux distribution (including Alpine and Scratch). |
| 7 | +You can check the example Makefile as well. |
| 8 | + |
| 9 | +### Create executables for both architectures (run **both** commands) |
| 10 | +```bash |
| 11 | +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o handler_amd64 main.go |
| 12 | +CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o handler_arm64 main.go |
| 13 | +``` |
| 14 | +Removing debugging information is not strictly necessary, but may help producing smaller executables. |
| 15 | + |
| 16 | +## 2. Packaging the fat zip |
| 17 | +```bash |
| 18 | +zip -j function.zip handler_amd64 handler_arm64 |
| 19 | +``` |
| 20 | + |
| 21 | +## 3. Deploy to Serverledge |
| 22 | +```bash |
| 23 | +# create the function |
| 24 | +bin/serverledge-cli create \ |
| 25 | + -f gofunc \ |
| 26 | + --memory 300 \ |
| 27 | + --src examples/go-example/go-function.zip \ |
| 28 | + --runtime go125 |
| 29 | + |
| 30 | +# and then execute it |
| 31 | +bin/serverledge-cli invoke \ |
| 32 | +-f gofunc \ |
| 33 | +-p "name:Serverledge User" \ |
| 34 | +-p "num:127" |
| 35 | + |
| 36 | +# Expected output |
| 37 | +{ |
| 38 | + "Success": true, |
| 39 | + "Result": "{\"message\":\"Hello Serverledge User\",\"number\":127,\"is_prime\":true}", |
| 40 | + "ResponseTime": 0.397966139, |
| 41 | + "IsWarmStart": false, |
| 42 | + "InitTime": 0.3957812, |
| 43 | + "QueueingTime": 0, |
| 44 | + "OffloadLatency": 0, |
| 45 | + "Duration": 0.002184848000000017, |
| 46 | + "Output": "" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Or with output capture enabled |
| 51 | +```bash |
| 52 | +bin/serverledge-cli invoke \ |
| 53 | +-f gofunc \ |
| 54 | +-p "name:Serverledge User" \ |
| 55 | +-p "num:127" \ |
| 56 | +-o # flag to capture the output |
| 57 | + |
| 58 | +# Expected output |
| 59 | +{ |
| 60 | + "Success": true, |
| 61 | + "Result": "{\"message\":\"Hello Serverledge User\",\"number\":127,\"is_prime\":true}", |
| 62 | + "ResponseTime": 0.001020982, |
| 63 | + "IsWarmStart": true, |
| 64 | + "InitTime": 0.000054193, |
| 65 | + "QueueingTime": 0, |
| 66 | + "OffloadLatency": 0, |
| 67 | + "Duration": 0.000966691, |
| 68 | + "Output": "2025/12/01 11:12:46 Processing request for user: Serverledge User...\n2025/12/01 11:12:46 Checked number: 127 (Prime: true)\n" |
| 69 | +} |
| 70 | +``` |
0 commit comments