What a Go App Needs Before You Deploy It (with a ShardCloud Walkthrough)

2026-11-30来源 dev.toEnglish

go run . works on your laptop. Then you upload your app and the build log says undefined: writeJSON. Or the build succeeds, but your public URL returns 502 Bad Gateway. Those failures happen at different points. In the first case, the platform never started the app. In the second, the proxy couldn't get a response from it.

We'll deploy a small API using Go's standard library, then use the build and startup logs to tell those two cases apart.

Know what the platform builds

  • A package build and a file build can include different code. go run . runs the package in your current directory, so it can pick up both main.go and handlers.go. With MAIN=main.go, ShardCloud builds that file as the entry point. This example keeps both routes in main.go. If your package spans several files, use a package build as shown below. ShardCloud Go Hosting Guide · Go command documentation
  • Ship go.mod with the source. Its go directive sets the minimum Go version the project needs. Choose a platform runtime at or above that version. If you add third-party dependencies, run go mod tidy and include the resulting go.sum when Go creates one. The example uses only the standard library. Go Modules Reference
  • Listen on the platform's port. Read PORT and use an address such as :8080. Leaving the host empty lets Go listen on all network interfaces. With 127.0.0.1:8080, the app can respond inside its container while the platform's proxy can't reach it. Go network documentation

Build a small Go API

Create a folder named hello-go. You'll need Go 1.27 or newer locally and a ShardCloud account for the upload.

go.mod

module example.com/hello-go go 1.27.0 

main.go

package main import ( "encoding/json" "log" "net" "net/http" "os" "time" ) func main() { port := os.Getenv("PORT") if port == "" { port = "8080" } mux := http.NewServeMux() mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]string{"message": "Hello from Go"}) }) mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]string{"status": "ok"}) }) addr := ":" + port listener, err := net.Listen("tcp", addr) if err != nil { log.Fatalf("Could not listen on %s: %v", addr, err) } server := &http.Server{ Handler: mux, ReadHeaderTimeout: 5 * time.Second, } log.Printf("Listening on %s", addr) log.Fatal(server.Serve(listener)) } func writeJSON(w http.ResponseWriter, body map[string]string) { w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(body); err != nil { log.Printf("Could not write response: %v", err) } } 

Run go run . from the project folder. With PORT unset, the log prints Listening on :8080. Open http://127.0.0.1:8080/ for the greeting and http://127.0.0.1:8080/health for {"status":"ok"}. The program logs its address after opening the listener, so that line confirms the port was bound.

Deploy the API to ShardCloud

Add .shardcloud next to go.mod and main.go.

DISPLAY_NAME=Hello Go MAIN=main.go MEMORY=512 VERSION=1.27.4 SUBDOMAIN=yourgoapi 

Replace yourgoapi with an available subdomain using letters and numbers. ShardCloud compiles the source and starts the resulting binary. Its Go runtime sets PORT=80, which the code reads on startup. ShardCloud Go Hosting Guide · ShardCloud configuration reference

Create a ZIP from inside the project folder. On macOS or Linux with zip installed, run this command.

zip hello-go.zip main.go go.mod .shardcloud 

On Windows, run this in PowerShell.

tar.exe -a -cf hello-go.zip main.go go.mod .shardcloud 

Both commands put the source and configuration at the archive root. In the ShardCloud dashboard, open Applications, choose New app, and upload hello-go.zip. Open the app's logs and look for Listening on :80. Then open /health on your https://<subdomain>.shardweb.app URL and check for HTTP 200 with {"status":"ok"}.

Diagnose a build failure or a 502

The build says undefined: writeJSON. If you've moved that helper to a second file in the same package, go run . can still work locally. go build main.go reproduces the file-only build. For a multi-file package at the project root, replace MAIN=main.go with these two lines in .shardcloud. ShardCloud configuration reference

LANGUAGE=go CUSTOM_COMMAND=go build -o app . && exec ./app 

The build says your module requires a newer Go version. Compare the go directive in go.mod with the selected runtime and choose a supported version that meets the requirement. Go Modules Reference

The build succeeds, but the URL returns 502. Compare the startup log with the runtime port. Listening on :8080 points to the local fallback. On ShardCloud, the example should log Listening on :80. If the port is correct, check that the listener accepts connections beyond loopback and that the dashboard shows the app running at the URL you're testing.

原文:https://www.a8q9.cn/en/article/what-a-go-app-needs-before-you-deploy-it-with-a-shardcloud-walkthrough-2c9209.html

相关文章