main
API
main
packageAPI reference for the main
package.
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
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)
}
}
Uses
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")
}