Get a role with the Go SDK
Permit.Api.Roles.Get() fetches one role and its details from the environment that your API key belongs to. This reference is for Go developers who read role-based access control (RBAC) roles from backend code. Use Get or GetByKey when you have the role key, and GetById when you have the role ID (a UUID).
Get a role by key with Roles.Get or Roles.GetByKey
Roles.GetByKey calls Roles.Get with the same arguments, so the two methods behave the same.
func (r *Roles) Get(ctx context.Context, roleKey string) (*models.RoleRead, error)
func (r *Roles) GetByKey(ctx context.Context, roleKey string) (*models.RoleRead, error)
Roles.Get parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
roleKey | string | Yes | The key of the role. |
Example: get a role by key
The example uses a client named Permit, created with permit.NewPermit() as shown in Check permissions with the Go SDK, and a ctx of type context.Context. Replace role-key with the key of your role:
role, err := Permit.Api.Roles.Get(ctx, "role-key")
Get a role by ID with Roles.GetById
Roles.GetById takes the role ID as a uuid.UUID from the github.com/google/uuid package, converts it to a string, and calls Roles.Get.
func (r *Roles) GetById(ctx context.Context, roleKey uuid.UUID) (*models.RoleRead, error)
Roles.GetById parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
roleKey | uuid.UUID | Yes | The ID of the role. The SDK names this parameter roleKey, but it takes the role ID. |
Example: get a role by ID
Pass the role ID as a uuid.UUID value. Parse an ID string with uuid.MustParse:
roleId := uuid.MustParse("<role-id>")
role, err := Permit.Api.Roles.GetById(ctx, roleId)
Role return value and errors
On success, each method returns a *models.RoleRead with the role's Key, Id, Name, Description, Permissions, and Extends.
If the call fails, err holds an errors.PermitError from the github.com/permitio/permit-golang/pkg/errors package. Its StatusCode field has the HTTP status, and its ErrorCode field has one of these codes:
ErrorCode | Cause |
|---|---|
NotFound | HTTP 404: no role with that key or ID exists in the environment. |
Unauthorized, ForbiddenAccess | HTTP 401 or 403: the API key is invalid or has no access to the environment. |
UnexpectedError | A server error (HTTP 5xx) or a network error. |