Skip to content
/ scrub Public

Recursively walk a Go struct and zero out specific fields

License

Notifications You must be signed in to change notification settings

acj/scrub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scrub

Recursively set specific struct fields to their zero values

Possible use cases:

  • scrubbing sensitive data from structs before logging
  • comparing structs with noisy fields (timestamps, random values, etc) for testing or diagnostic purposes

Examples

Using struct tags

package main

import (
  "fmt"

  "github.com/acj/scrub"
)

type User struct {
  Name string
  Age  int    `scrub:"true"`
}

func main() {
  user := User{
    Name: "Wall-E",
    Age:  22,
  }
  scrub.TaggedFields(&user)
  fmt.Printf("%+v\n", user) // {Name:Wall-E Age:0}
}

Using named fields (blocklist)

package main

import (
  "fmt"

  "github.com/acj/scrub"
)

type User struct {
  Name string
  Age  int
}

func main() {
  user := User{
    Name: "Wall-E",
    Age:  22,
  }
  scrub.NamedFields(&user, "Age")
  fmt.Printf("%+v\n", user) // {Name:Wall-E Age:0}
}

License

MIT

About

Recursively walk a Go struct and zero out specific fields

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages