Перейти до вмісту
Docs Try Aspire
Docs Try

Go integration

Цей контент ще не доступний вашою мовою.

⭐ Community Toolkit Go logo

The Aspire Go hosting integration enables you to run Go applications alongside your other Aspire resources in the app host. Go apps participate in the same service discovery, health checks, OpenTelemetry export, and Aspire dashboard support as the rest of your solution.

To access the Go hosting APIs in your AppHost project, install the 📦 CommunityToolkit.Aspire.Hosting.Golang NuGet package:

Aspire CLI — Додати пакет CommunityToolkit.Aspire.Hosting.Golang
aspire add communitytoolkit-golang

Aspire CLI інтерактивний; оберіть відповідний результат пошуку:

Aspire CLI — Приклад виводу
Select an integration to add:
> communitytoolkit-golang (CommunityToolkit.Aspire.Hosting.Golang)
> Other results listed as selectable options...

Add a Go application to your app host using the AddGolangApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp("go-api", "../go-api")
.WithHttpEndpoint(env: "PORT")
.WithHttpHealthCheck("/health");
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(goApp);
builder.Build().Run();

AddGolangApp requires:

  • name: The name of the resource in the Aspire dashboard.
  • workingDirectory: The path to the directory containing your Go application, relative to the AppHost project. The app host runs go run <executable> in this directory.

By default, the executable target is . (the Go module root), which is equivalent to go run ..

Pass an explicit executable argument to run a package at a specific path inside the working directory:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp(
name: "go-api",
workingDirectory: "../go-api",
executable: "./cmd/server")
.WithHttpEndpoint(env: "PORT");
builder.Build().Run();

Go applications typically read the port from an environment variable. Use WithHttpEndpoint to declare the HTTP endpoint and bind it to a named environment variable:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp("go-api", "../go-api")
.WithHttpEndpoint(port: 8080, env: "PORT");
builder.Build().Run();

Your Go application reads the PORT variable at startup:

Go — main.go
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
})
fmt.Printf("Server listening on port %s\n", port)
http.ListenAndServe(":"+port, nil)
}

Call WithGoModTidy to run go mod tidy before the Go app starts. This ensures the module’s dependency graph is consistent and removes unused entries from go.mod and go.sum:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp("go-api", "../go-api")
.WithGoModTidy()
.WithHttpEndpoint(env: "PORT");
builder.Build().Run();

Call WithGoModDownload to run go mod download instead. This downloads all module dependencies to the local module cache without modifying go.mod or go.sum:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp("go-api", "../go-api")
.WithGoModDownload()
.WithHttpEndpoint(env: "PORT");
builder.Build().Run();

Both methods run only in run mode and have no effect during publish.

Pass buildTags to control conditional compilation in the Go source:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var goApp = builder.AddGolangApp(
name: "go-api",
workingDirectory: "../go-api",
executable: ".",
buildTags: ["integration"])
.WithHttpEndpoint(env: "PORT");
builder.Build().Run();

The build tags are passed to go run as -tags integration and to the generated Dockerfile’s build stage as go build -tags integration.