Golang Masker is a simple utility of creating a mask for sensitive information.
- Changes have been made according to the original version.
- Getting Started
$ go get -u github.com/rosbit/go-masker
There are two ways to get a masker instance:
package main
import (
masker "github.com/rosbit/go-masker"
)
func main() {
masker.Name("rosbit")
masker.ID("110101198012310070")
masker.Mobile("13010010010")
}
package main
import (
masker "github.com/rosbit/go-masker"
)
func main() {
m := masker.New() // or m := masker.New("*")
m.Name("rosbit")
m.ID("110101198012310070")
m.Mobile("13010010010")
}
Type | Const | Tag | Description |
---|---|---|---|
Name | MName | name | mask the second letter and the third letter |
Password | MPassword | password | always return ************ |
Address | MAddress | addr | keep first 6 letters, mask the rest |
MEmail | keep domain and the first 3 letters | ||
Mobile | MMobile | mobile | mask 4 digits from the 4'th digit |
Telephone | MTelephone | tel | remove ( , ) , , - chart, and mask last 4 digits of telephone number, format to (??)????-???? |
ID | MID | id | mask 8 digits from the 6'th digit of ID number |
CreditCard | MCreditCard | credit | mask 6 digits from the 7'th digit |
Struct | MStruct | struct | mask the struct |
String
methomd requires two parameters, a mask type CONST and a string:
package main
import (
masker "github.com/rosbit/go-masker"
)
func main() {
masker.String(masker.MName, "rosbit")
masker.String(masker.MID, "110101198012310070")
masker.String(masker.MMobile, "13010010010")
}
Result:
r**bit
110101********0070
130****0010
package main
import (
masker "github.com/rosbit/go-masker"
)
func main() {
masker.String(masker.MName, "rosbit")
masker.String(masker.MID, "110101198012310070")
masker.SetMask("-")
masker.String(masker.MMobile, "13010010010")
}
Result:
r**bit
110101********0070
130----0010
You can define your struct and add tag mask
to let masker know what kind of the format to mask.
Field must be public in the struct.
package main
import (
"log"
masker "github.com/rosbit/go-masker"
)
type Foo struct {
Name string `mask:"name"`
Mobile string `mask:"mobile"`
}
func main() {
foo := &Foo{
Name: "rosbit",
Mobile: "13010010010",
}
t, err := masker.Struct(foo)
log.Println(t)
log.Println(t.(*Foo))
log.Println(err)
}
Result:
t = &{r**bit 130****0010}
err = <nil>
package main
import (
masker "github.com/rosbit/go-masker"
)
type Foo struct {
Name string `mask:"name"`
Mobile string `mask:"mobile"`
Qoo *Qoo `mask:"struct"`
}
type Qoo struct {
Name string `mask:"name"`
Telephone string `mask:"tel"`
}
func main() {
foo := &Foo{
Name: "ggwhite",
Mobile: "0987987987",
Qoo: &Qoo{
Name: "gino",
Telephone: "0287658765",
},
}
t, err := masker.Struct(foo)
log.Println(t)
log.Println(t.(*Foo).Qoo)
log.Println(err)
}
Result:
t = &{g**hite 0987***987 0xc00000a080}
t.Qoo = &{g**o (02)8765-****}
err = <nil>
package main
import (
masker "github.com/rosbit/go-masker"
)
type Foo struct {
Name string `mask:"name"`
Mobile string `mask:"mobile"`
IDs []string `mask:"id"`
}
func main() {
foo := &Foo{
Name: "ggwhite",
Mobile: "0987987987",
IDs: []string{
"A123456789",
"A987654321",
},
}
t, err := masker.Struct(foo)
log.Println(t)
log.Println(err)
}
Result:
t = &{g**hite 0987***987 [A12345**** A98765****]}
err = <nil>