Basic Types
Censor provides comprehensive support for Go's basic types. This guide details how each basic type is handled by Censor.
String
Strings are masked by default, but can be configured to be displayed using struct tags.
type User struct {
ID string `censor:"display"` // Always visible.
Email string // Masked by default.
Password string `censor:"mask"` // Explicitly masked.
}
user := User{
ID: "123",
Email: "user@example.com",
Password: "secret123",
}
// TEXT output: {ID:123 Email:[CENSORED] Password:[CENSORED]}.
// JSON output: {"ID":"123","Email":"[CENSORED]","Password":"[CENSORED]"}.
String Patterns
You can exclude specific string patterns from masking:
c := censor.New(
censor.WithExcludePatterns([]string{
`\d{4}-\d{4}-\d{4}-\d{4}`, // Credit card numbers.
`\d{3}-\d{2}-\d{4}`, // SSN.
}),
)
// Credit card number will not be masked.
card := "4111-1111-1111-1111"
// Output: 4111-1111-1111-1111.
// SSN will not be masked.
ssn := "123-45-6789"
// Output: 123-45-6789.
// Other strings will still be masked.
email := "user@example.com"
// Output: [CENSORED].
Numeric Types
Numeric types are displayed as is, without masking.
Integers
type User struct {
ID string `censor:"display"`
Age int
Count int8
Score int16
Points int32
Balance int64
}
user := User{
ID: "123",
Age: 30,
Count: 42,
Score: 100,
Points: 1000,
Balance: 1000000,
}
// TEXT output: {ID:123 Age:30 Count:42 Score:100 Points:1000 Balance:1000000}.
// JSON output: {"ID":"123","Age":30,"Count":42,"Score":100,"Points":1000,"Balance":1000000}.
Unsigned Integers
type User struct {
ID string `censor:"display"`
Count uint
Small uint8
Medium uint16
Large uint32
Huge uint64
}
user := User{
ID: "123",
Count: 42,
Small: 255,
Medium: 65535,
Large: 4294967295,
Huge: 18446744073709551615,
}
// TEXT output: {ID:123 Count:42 Small:255 Medium:65535 Large:4294967295 Huge:18446744073709551615}.
// JSON output: {"ID":"123","Count":42,"Small":255,"Medium":65535,"Large":4294967295,"Huge":18446744073709551615}.
Floating Point
type User struct {
ID string `censor:"display"`
Score float32
Balance float64
}
user := User{
ID: "123",
Score: 98.5,
Balance: 1234.5678,
}
// TEXT output: {ID:123 Score:98.5 Balance:1234.5678}.
// JSON output: {"ID":"123","Score":98.5,"Balance":1234.5678}.
Boolean
Boolean values are displayed as is, without masking.
type User struct {
ID string `censor:"display"`
Active bool
Verified bool
}
user := User{
ID: "123",
Active: true,
Verified: false,
}
// TEXT output: {ID:123 Active:true Verified:false}.
// JSON output: {"ID":"123","Active":true,"Verified":false}.
Time
Time values are formatted according to the configuration.
type User struct {
ID string `censor:"display"`
CreatedAt time.Time
UpdatedAt time.Time
}
user := User{
ID: "123",
CreatedAt: time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC),
UpdatedAt: time.Date(2023, 1, 2, 12, 0, 0, 0, time.UTC),
}
// With default time format.
// TEXT output: {ID:123 CreatedAt:2023-01-01T12:00:00Z UpdatedAt:2023-01-02T12:00:00Z}.
// JSON output: {"ID":"123","CreatedAt":"2023-01-01T12:00:00Z","UpdatedAt":"2023-01-02T12:00:00Z"}.
// With custom time format.
c := censor.New(
censor.WithTimeFormat("2006-01-02"),
)
// TEXT output: {ID:123 CreatedAt:2023-01-01 UpdatedAt:2023-01-02}.
// JSON output: {"ID":"123","CreatedAt":"2023-01-01","UpdatedAt":"2023-01-02"}.
Pointers
Pointers are dereferenced and processed according to their underlying type.
type User struct {
ID *string `censor:"display"`
Email *string
Password *string `censor:"mask"`
}
id := "123"
email := "user@example.com"
password := "secret123"
user := User{
ID: &id,
Email: &email,
Password: &password,
}
// TEXT output: {ID:123 Email:[CENSORED] Password:[CENSORED]}.
// JSON output: {"ID":"123","Email":"[CENSORED]","Password":"[CENSORED]"}.
Nil Values
Nil values are handled appropriately for each type.
type User struct {
ID *string `censor:"display"`
Email *string
Password *string `censor:"mask"`
}
user := User{
ID: nil,
Email: nil,
Password: nil,
}
// TEXT output: {ID:<nil> Email:<nil> Password:<nil>}.
// JSON output: {"ID":null,"Email":null,"Password":null}.
Complete Example
Here's a complete example showing how Censor handles various basic types:
package main
import (
"fmt"
"time"
"github.com/vpakhuchyi/censor"
)
func main() {
// Create a Censor instance with custom configuration
c := censor.New(
censor.WithMaskValue("[REDACTED]"),
censor.WithTimeFormat("2006-01-02"),
)
// Define a struct with various basic types
type User struct {
ID string `censor:"display"`
Email string
Password string `censor:"mask"`
Age int
Score float64
Active bool
CreatedAt time.Time
UpdatedAt *time.Time
APIKey *string
}
// Create a user with various basic types
updatedAt := time.Date(2023, 1, 2, 12, 0, 0, 0, time.UTC)
apiKey := "sk_live_123456789"
user := User{
ID: "123",
Email: "user@example.com",
Password: "secret123",
Age: 30,
Score: 98.5,
Active: true,
CreatedAt: time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC),
UpdatedAt: &updatedAt,
APIKey: &apiKey,
}
// Process the data
masked := c.Process(user)
fmt.Printf("%+v\n", masked)
// TEXT output: {ID:123 Email:[REDACTED] Password:[REDACTED] Age:30 Score:98.5 Active:true CreatedAt:2023-01-01 UpdatedAt:2023-01-02 APIKey:[REDACTED]}
// JSON output: {"ID":"123","Email":"[REDACTED]","Password":"[REDACTED]","Age":30,"Score":98.5,"Active":true,"CreatedAt":"2023-01-01","UpdatedAt":"2023-01-02","APIKey":"[REDACTED]"}
}
Next Steps
- Learn about Complex Types
- Check out Special Types
- See Format-Specific handling