Skip to main content

Simple Examples

Here are some basic examples to help you get started with Censor.

Basic String Masking

The simplest use case is masking sensitive data in strings:

package main

import (
"fmt"
"github.com/vpakhuchyi/censor"
)

func main() {
// Using the default global instance
text := "My password is secret123"
fmt.Println(censor.Format(text))
// Output: My password is [CENSORED]
}

Struct Field Masking

Control which fields are visible using struct tags:

package main

import (
"fmt"
"github.com/vpakhuchyi/censor"
)

type User struct {
ID string `censor:"display"` // Will be visible
Username string `censor:"display"` // Will be visible
Email string // Will be masked
Password string // Will be masked
}

func main() {
user := User{
ID: "123",
Username: "johndoe",
Email: "john@example.com",
Password: "secret123",
}

fmt.Println(censor.Format(user))
// Output: {ID: 123, Username: johndoe, Email: [CENSORED], Password: [CENSORED]}
}

Map Handling

Censor can handle maps with various key and value types:

package main

import (
"fmt"
"github.com/vpakhuchyi/censor"
)

func main() {
// Simple map
data := map[string]string{
"username": "johndoe",
"password": "secret123",
"email": "john@example.com",
}

fmt.Println(censor.Format(data))
// Output: map[email: [CENSORED], password: [CENSORED], username: johndoe]

// Nested map
nested := map[string]interface{}{
"user": map[string]string{
"id": "123",
"password": "secret",
},
}

fmt.Println(censor.Format(nested))
// Output: map[user: map[id: 123, password: [CENSORED]]]
}

Slice Processing

Working with slices and arrays:

package main

import (
"fmt"
"github.com/vpakhuchyi/censor"
)

func main() {
// Slice of sensitive data
emails := []string{
"user1@example.com",
"user2@example.com",
}

fmt.Println(censor.Format(emails))
// Output: [[CENSORED], [CENSORED]]

// Slice of structs
users := []User{
{ID: "1", Username: "john", Email: "john@example.com"},
{ID: "2", Username: "jane", Email: "jane@example.com"},
}

fmt.Println(censor.Format(users))
// Output: [{ID: 1, Username: john, Email: [CENSORED]}, {ID: 2, Username: jane, Email: [CENSORED]}]
}

Custom Configuration

Simple configuration example:

package main

import (
"fmt"
"github.com/vpakhuchyi/censor"
)

func main() {
// Create a custom configuration
cfg := censor.Config{
Formatter: censor.FormatterConfig{
MaskValue: "***",
ExcludePatterns: []string{
`\w+@\w+\.\w+`, // Email pattern
},
},
}

// Create a new instance with the configuration
c := censor.NewWithOpts(censor.WithConfig(&cfg))

data := "Contact us at support@example.com"
fmt.Println(c.Format(data))
// Output: Contact us at ***
}

Basic Logging Integration

Simple example with standard logging:

package main

import (
"log"
"github.com/vpakhuchyi/censor"
)

func main() {
user := User{
ID: "123",
Username: "johndoe",
Email: "john@example.com",
Password: "secret123",
}

// Log with sensitive data masked
log.Printf("Processing user: %s", censor.Format(user))
// Output: Processing user: {ID: 123, Username: johndoe, Email: [CENSORED], Password: [CENSORED]}
}

Best Practices for Simple Usage

  1. Default Configuration: Start with default configuration for simple cases
  2. Struct Tags: Use censor:"display" tags to explicitly mark safe fields
  3. Type Safety: Let Censor handle type conversion and formatting
  4. Consistent Usage: Use censor.Format() consistently across your application
  5. Error Handling: Always check for errors when using custom configuration