Get started with the Go SDK
このページはまだあなたの言語に翻訳されていません。 現在取り組んでいますので、後でもう一度確認してください。
Install the Go SDK, create a client, and send one event from your Go program to Amplitude. The SDK is the Go module github.com/amplitude/analytics-go, and its package name is amplitude. Each step lists the conditions that commonly break that step, so you can fix a problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the Go SDK releases on GitHub.
The Go SDK is for server-side code that sends events on behalf of many users. It doesn't store a user ID, device ID, or session between calls, so you pass the user's identity with each event.
Prerequisites
- An Amplitude project and its API key. Find the key in Settings > Projects > your project > General.
- The data center your organization uses: US (default) or EU.
- The latest Go SDK release. If you pin an older version, check the Go SDK releases to confirm that it includes the features you use.
- Go 1.17 or later, with Go modules. The examples on these pages run on Go 1.27.
Install the SDK
From the directory that holds your go.mod, add the module:
go get github.com/amplitude/analytics-go
Then import the amplitude package, which holds the client, the event types, and the configuration:
package main
import "github.com/amplitude/analytics-go/amplitude"
func main() {
_ = amplitude.NewConfig("AMPLITUDE_API_KEY")
}
Fix installation problems
| Symptom | Cause | Fix |
|---|---|---|
no required module provides package github.com/amplitude/analytics-go/amplitude; to add it: | The module isn't in your go.mod. | Run go get github.com/amplitude/analytics-go in the directory that holds go.mod, and commit go.mod and go.sum. |
undefined: amplitude.IdentityOpSet, or another amplitude.IdentityOp... constant | The identity operation constants live in the types subpackage. The amplitude package re-exports the IdentityOp type but not its constants. | Use the Identify methods such as Set(), or import github.com/amplitude/analytics-go/amplitude/types and use types.IdentityOpSet. |
Initialize the SDK
Create a Config with your API key, change any options on it, then pass it to NewClient(). Create one client when your process starts and share it across requests and goroutines. Defer Shutdown() so the client sends queued events before the program exits.
package main
import "github.com/amplitude/analytics-go/amplitude"
func main() {
config := amplitude.NewConfig("AMPLITUDE_API_KEY")
client := amplitude.NewClient(config)
defer client.Shutdown()
}
NewClient() copies the Config. Changes you make to config after the call don't affect the client.
If your project uses the EU data center, set config.ServerZone = amplitude.ServerZoneEU before you call NewClient(). For other options, go to Configuration options.
Fix initialization problems
| Symptom | Cause | Fix |
|---|---|---|
Uploads fail with Invalid API key | The key is empty or wrong, or belongs to a project in the other data center. NewClient() doesn't check the key, so the error appears only when the SDK uploads events. | Check the environment variable that holds your key, and set ServerZone to match the project's data center. |
| The SDK uploads far more often than expected, or the log fills with requests | FlushInterval is a time.Duration. A bare number such as 5000 means 5000 nanoseconds. | Write durations with units: config.FlushInterval = 5 * time.Second. |
| Options you set have no effect | You changed config after NewClient(). | Set every option before NewClient(). To check what the client uses, call client.Config(). |
Send a first event
Track one event for a user. Set UserID (or DeviceID) on every event, because the SDK doesn't remember identity between calls. Track() queues the event and returns immediately. The deferred Shutdown() sends the event before main returns.
package main
import "github.com/amplitude/analytics-go/amplitude"
func main() {
config := amplitude.NewConfig("AMPLITUDE_API_KEY")
client := amplitude.NewClient(config)
defer client.Shutdown()
client.Track(amplitude.Event{
EventType: "Sign Up Completed",
UserID: "user-12345",
EventProperties: map[string]interface{}{"plan": "starter"},
})
}
The SDK sends this event to Amplitude:
{
"event_type": "Sign Up Completed",
"user_id": "user-12345",
"event_properties": { "plan": "starter" },
"time": 1794431065800,
"insert_id": "d99fc12f-77d3-40c5-9bb6-6a082fdd0a80",
"library": "amplitude-go/1.3.1"
}
In a long-running server, the SDK uploads queued events every 10 seconds, or as soon as 200 events are waiting. Go to How the SDK uploads events.
Fix upload problems
The default logger writes every upload result to standard error, with the prefix amplitude-analytics - . To get the result in code, set an ExecuteCallback.
| Log line on standard error | Cause | Fix |
|---|---|---|
| No upload lines at all, and the program exits | The program exited before the next upload, without calling Shutdown() or Flush(). The SDK holds events only in memory. | Call defer client.Shutdown() immediately after NewClient(). Go to Flush before the process exits. |
Error: Invalid event, EventType and either UserID or DeviceID cannot be empty | The event has no EventType, or neither UserID nor DeviceID. The SDK sends the event anyway, and Amplitude rejects it. | Set EventType and UserID or DeviceID on every event. |
Error: Invalid API key: code=400, ... | The key is wrong, or belongs to a project in the other data center. The SDK drops every event in the request. | Copy the key again and check ServerZone. |
Error: Invalid id length for user_id or device_id: code=400, ... | An ID is shorter than five characters, Amplitude's default minimum. | Use longer IDs, or set MinIDLength. |
Error: HTTP request failed: Post "https://api2.amplitude.com/2/httpapi": ... code=0, ... | The request never reached a server: DNS failure, a firewall, or no outbound network. The SDK drops these events without retrying. | Confirm that the host can reach api2.amplitude.com (or api.eu.amplitude.com) on port 443. If outbound traffic must go through a proxy, set ServerURL. |
Error: Event reached max retry times 12: code=500, ... | Amplitude returned server errors or timed out on every attempt. | Check the Info: HTTP response code lines before it for the status of each attempt. |
Confirm the event in Amplitude
- In Amplitude, open User Lookup and search for the user ID you passed,
user-12345in the example. - Open the Activity tab and turn on Live event updates.
- Confirm that
Sign Up Completedappears with theplanproperty.
If the event doesn't appear within a minute, follow Verify events. That page checks each stage between Track() and Amplitude's servers.
Next steps
- Add the events and properties your tracking plan needs. Go to Track events.
- Keep the SDK from logging your API key in production. Go to Control logging.
- Flush events before a job, CLI, or serverless function exits. Go to Flush before the process exits.
- Tune batching, retries, and timeouts. Go to Data delivery.
これは役に立ちましたか?
For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.