go-faker

go-faker can generate all kinds of mock data, which is good for testing scenarios.

For example, when you need a fake user record in order to complete some kind of registration process unit test, you can use.

1
2
> docker run -it --rm hedzr/faker internet
{"Internet":{"DomainName":"ziemann.net","DomainSuffix":"com","DomainWord":"green","Email":"garry@hintz.biz","FreeEmail":"christian@yahoo.com","IpV4Address":"48.107.223.211","IpV6Address":"bc8e:e770:c962:4004:8045:86e2:2658:bfc7","MacAddress":"52:6b:0d:24:7d:67","Password":"Za9pR1hfV7yJXw3Mhf9I5EvL1uvo0M","SafeEmail":"sheridan.gibson@example.net","Slug":"adipisci-inventore","Url":"http://roobroberts.name/americo.lindgren","UserName":"lenny.trantow"}}

This gives you a user record with a limited but almost sufficient number of fields.

Installation

Using the docker run will work immediately, as long as you can pull the image from the docker hub without any problems.

1
docker pull hedzr/faker

If you prefer to pull mirrors from GitHub’s Registry, that’s fine.

1
docker pull ghcr.io/hedzr/cli/faker

If you are using a macOS environment, you can install it by way of Homebrew at

1
2
3
4
5
6
7
8
brew tap hedzr/brew
brew install faker
# 或者直接:
brew install hedzr/brew/faker

# 然后
faker --help
...

However, the M1 environment may not be supported.

For other OS environments, download the pre-compiled binary package from the Releases page.

If there is no pre-compiled binary package, you can only get your executable by compiling the source code, which requires that you have the golang compiler environment installed at

1
go install github.com/hedzr/go-faker

There is always something for you in the above approaches.

Background

The go-faker is implemented via dmgk/faker. Our version is just a shell of an executable with a proper wrapper.

go-faker currently supports almost all dmgk/faker data, except for date and time, both of which are easier to mock using the OS’s own date command.

The available go-faker commands are as follows.

1
> faker -h

It can be used as follows.

1
2
> faker addr
{"Address":{"BuildingNumber":788,"City":"East Geraldineside","CityPrefix":"North","CitySuffix":"land","Country":"Costa Rica","CountryCode":"BG","Latitude":82.16658,"Longitude":-89.001114,"Postcode":"95690-3954","PostcodeByState":64204,"SecondaryAddress":"Apt. 211","State":"Rhode Island","StateAbbr":"OR","StreetAddress":"10067 Jimmy Forge","StreetName":"Gleichner Vista","StreetSuffix":"Shoals","String":"95126 Kamren Haven Apt. 514, Port Joannyburgh Utah 96447","TimeZone":"Africa/Cairo","ZipCode":"58451-9050","ZipCodeByState":38836}}

You can also output yaml format, etc.

Command Line Arguments

Proper overwrap means that we have a set of command line arguments that are intuitively idiomatic and conform to POSIX conventions for supplying them.

As an example, a credit card number might look like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
❯ ./bin/faker_darwin-amd64 f --visa --plain # shortcut to 'finance'
        visa : 4364442188476

❯ ./bin/faker_darwin-amd64 finance -m
    mastercard : 6771-8918-3284-3326

❯ ./bin/faker_darwin-amd64 finance --mastercard
{ "finance": { "mastercard": "5150-3577-9332-4378" } }

❯ ./bin/faker_darwin-amd64 finance --mastercard -yaml
finance:
  mastercard: 5480-4039-7556-4612

❯ ./bin/faker_darwin-amd64 finance --mastercard --json
{
  "finance": {
    "mastercard": "6771-8948-9019-6694"
  }
}

You can choose the desired output format, json or json-compact, or yaml format.

For the finance command, you can output credit card numbers in the following format.

Please make your own choice.

Other faked records are listed in the same way, so they are not listed here.

You can check the home pages of go-faker and dmgk/faker respectively for more information.