Get started with the Go SDK

이 페이지는 아직 귀하의 언어로 번역되지 않았습니다. 현재 작업 중이므로 곧 다시 확인해 주십시오.

For Go developers10 minBeginner

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:

bash
go get github.com/amplitude/analytics-go

Then import the amplitude package, which holds the client, the event types, and the configuration:

go
package main

import "github.com/amplitude/analytics-go/amplitude"

func main() {
	_ = amplitude.NewConfig("AMPLITUDE_API_KEY")
}

Fix installation problems

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.

go
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

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.

go
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:

json
{
  "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.

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the user ID you passed, user-12345 in the example.
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Completed appears with the plan property.

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

이 내용이 도움이 되었나요?

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.

이 페이지에서