Last Updated: 11/9/2022, 9:14:41 AM

# Golang SDK Getting Started Guide

# Overview

You can use AccelByte Cloud’s Golang SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. This guide will show you how to create an application that uses Golang Server SDK from scratch.

# Prerequisites

# Tutorial

# Create a Golang Project

Create a folder and use go mod init to create a Golang project.

$ mkdir myproject
$ cd myproject/
$ go mod init golang-application

# Add to Project Dependency

  1. Add the required configuration in go.mod.

  2. Replace {VERSION} with a specific release version tag (opens new window)

  3. Run go mod tidy.

    module golang-application
    
    go 1.16
    
    require (
        github.com/AccelByte/accelbyte-go-sdk {VERSION}
    )
    

    TIP

    We recommended using the Golang Server SDK version that matches your AccelByte Cloud version.

# Use in Code

  1. Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in main.go.

  2. Create the file main.go inside the project directory.

  3. The ConfigRepositoryImpl gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.

    package main
    
    import (
    	"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient/o_auth2_0_extension"
    	"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils/auth"
    	"github.com/sirupsen/logrus"
    
    	"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/factory"
    	"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/service/iam"
    )
    
    var (
    	// use the default config and token implementation
    	configRepo = *auth.DefaultConfigRepositoryImpl()
    	tokenRepo  = *auth.DefaultTokenRepositoryImpl()
    )
    
    func main() {
    	// prepare the IAM Oauth service
    	oauth := &iam.OAuth20Service{
    		Client:           factory.NewIamClient(&configRepo),
    		ConfigRepository: &configRepo,
    		TokenRepository:  &tokenRepo,
    	}
    	clientId := oauth.ConfigRepository.GetClientId()
    	clientSecret := oauth.ConfigRepository.GetClientSecret()
    
    	// call the endpoint tokenGrantV3Short through the wrapper 'LoginClient'
    	err := oauth.LoginClient(&clientId, &clientSecret)
    	if err != nil {
    		logrus.Error("failed login client")
    	} else {
    		logrus.Info("successful login")
    	}
    
    	// get the token
    	token, _ := oauth.TokenRepository.GetToken()
    	logrus.Infof("print %v", *token.AccessToken)
    
    	// prepare the IAM's Oauth 2.0 Extention service
    	oAuth20ExtensionService := &iam.OAuth20ExtensionService{
    		Client:          factory.NewIamClient(&configRepo),
    		TokenRepository: &tokenRepo,
    	}
    	input := &o_auth2_0_extension.GetCountryLocationV3Params{}
    
    	// call an AccelByte Cloud API e.g. GetCountryLocationV3
    	ok, errLoc := oAuth20ExtensionService.GetCountryLocationV3Short(input)
    	if errLoc != nil {
    		logrus.Error(errLoc.Error())
    	} else {
    		logrus.Infof("Country name: %s", *ok.CountryName)
    	}
    }
    

# Run the Code

Set the required environment variables and run the code using go run main.go.

$ export AB_BASE_URL="https://demo.accelbyte.io"              # AccelByte Cloud Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"      # AccelByte Cloud OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # AccelByte Cloud OAuth Client Secret
$ go run main.go

# Import AccelByte Services

Now you’re ready to integrate any of the following AccelByte services into your application. All services have already been imported in .../pkg/service.<your-service> will be changed to the service name you want to use automatically during the import.

# SDK Examples

See our Golang SDK example repo (opens new window) for a selection of test cases you can use to customize your game.

IAM

API Docs (opens new window)

SDK Reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
)
Basic

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/basic-sdk/pkg/basicclientmodels"
)
Social

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/social-sdk/pkg/socialclientmodels"
)
Platform

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/platform-sdk/pkg/platformclientmodels"
)
Group

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/group-sdk/pkg/groupclientmodels"
)
Cloud Save

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/cloudsave-sdk/pkg/cloudsaveclientmodels"
)
DSM Controller

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/dsmc-sdk/pkg/dsmcclientmodels"
)
Session Browser

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/sessionbrowser-sdk/pkg/sessionbrowserclientmodels"
)
Lobby

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/lobby-sdk/pkg/lobbyclientmodels"
)
Achievement

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/achievement-sdk/pkg/achievementclientmodels"
)
DS Log Manager

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/dslogmanager-sdk/pkg/dslogmanagerclientmodels"
)
UGC

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/ugc-sdk/pkg/ugcclientmodels"
)
Leaderboard

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/leaderboard-sdk/pkg/leaderboardclientmodels"
)
GDPR

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/gdpr-sdk/pkg/gdprclientmodels"
)
Legal

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/legal-sdk/pkg/legalclientmodels"
)
Matchmaking

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/matchmaking-sdk/pkg/matchmakingclientmodels"
)