Help us shape the future of Go! Take the 2026 Go Developer Survey

redacted

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 1 Imported by: 1

README

redacted-string

Go Report Card PkgGoDev

This is a very simple module that can be used to print a string as all '*' characters, although this is configurable.

Install

go get github.com/andrewheberle/redacted-string

Usage

Usage can be as simple as using the Redact call as follows:

fmt.Println(redacted.Redact("a sensitive string"))
// Output: ********

Or you may customise the behaviour as follows:

censor := redacted.New("a sensitive string", redacted.WithCharacter('!'), redacted.WithLength(12))
// Output: !!!!!!!!!!!!

Documentation

Index

Examples

Constants

View Source
const (
	// The default character to use in place of real characters in a redacted string for a Censor created via New.
	RedactedCharacter rune = '*'
	// The default maximum length of the redacted string for a Censor created via New. A value of zero (0) means the redacted string will be the same length as the original.
	RedactedLength uint = 8
)

Variables

This section is empty.

Functions

func Redact

func Redact(s string, opts ...Option) string

Redact can be used to print a redacted string (ie printed as all '********')

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/redacted-string"
)

func main() {
	s := "this is a string"
	fmt.Println(redacted.Redact(s))
}
Output:
********

Types

type Censor

type Censor struct {
	// contains filtered or unexported fields
}

Censor allows you to hide sensitive values. This should be created using New rather than directly.

func New

func New(s string, opts ...Option) *Censor

New creates a new Censor that can be used to redact the provided string

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/redacted-string"
)

func main() {
	s := "this is a string"

	censor := redacted.New(s)
	fmt.Println(censor)
}
Output:
********

func (*Censor) Set

func (c *Censor) Set(s string)

Set changes (or sets) the string to be redacted

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/redacted-string"
)

func main() {
	s := "this is a string"

	censor := redacted.New(s, redacted.WithLength(0))
	censor.Set("a new string that we want to be fully replaced with the * character")
	fmt.Println(censor)
}
Output:
*******************************************************************

func (Censor) String

func (c Censor) String() string

type Option

type Option func(*Censor)

Option type to change the behaviour of a Censor

func WithCharacter

func WithCharacter(char rune) Option

WithCharacter sets the character

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/redacted-string"
)

func main() {
	s := "this is a string"

	censor := redacted.New(s, redacted.WithCharacter('.'))
	fmt.Println(censor)
}
Output:
........

func WithLength

func WithLength(length uint) Option

WithLength sets the length of the redacted string. A value of zero means the redacted version is the same length as the original.

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/redacted-string"
)

func main() {
	s := "this is a string"

	censor := redacted.New(s, redacted.WithLength(12))
	fmt.Println(censor)
}
Output:
************

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL