main API

main

package

API reference for the main package.

S
struct

User

examples/basic/main.go:9-12
type User struct

Methods

GetID
Method

Returns

string
func (User) GetID() string
{ return u.ID }
GetRoles
Method

Returns

[]string
func (User) GetRoles() []string
{ return u.Roles }

Fields

Name Type Description
ID string
Roles []string
S
struct

Post

Post represents a resource protected by go-guard.

examples/basic/main.go:18-24
type Post struct

Fields

Name Type Description
AuthorID string guard:"role:owner"
Content string guard:"read:owner,admin,guest; edit:owner,admin; delete:admin"
F
function

check

Parameters

u
p
action
string
examples/basic/main.go:26-32
func check(u User, p *Post, action string)

{
	if err := guard.Can(u, p, action); err != nil {
		fmt.Printf("[DENY]  User %s cannot %s: %v\n", u.ID, action, err)
	} else {
		fmt.Printf("[ALLOW] User %s can %s\n", u.ID, action)
	}
}
F
function

main

examples/basic/main.go:34-53
func main()

{
	post := &Post{AuthorID: "alice", Content: "Hello World"}

	alice := User{ID: "alice", Roles: []string{"user"}}
	bob := User{ID: "bob", Roles: []string{"guest"}}
	admin := User{ID: "admin", Roles: []string{"admin"}}

	fmt.Println("--- Alice (Owner) ---")
	check(alice, post, "read")
	check(alice, post, "edit")
	check(alice, post, "delete")

	fmt.Println("\n--- Bob (Guest) ---")
	check(bob, post, "read")
	check(bob, post, "edit")

	fmt.Println("\n--- Admin ---")
	check(admin, post, "delete")
	check(admin, post, "edit")
}