Gofakeit
是一款Go语言编写的随机数据生成工具,使用更易用的接口设计,可满足日常开发中绝大多数数据生成场景
Gofakeit
让您以更优雅的姿势生成测试数据。Gofakeit
内置160+函数,同时也很方便自定义。
功能
160+内置函数!!!
多个rand源
全局rand
支持struct tag
支持自定义函数
内置http server
内置command line tool
0依赖
高性能
安装
import "github.com/brianvoe/gofakeit/v6"
Usage
快速开始
import "github.com/brianvoe/gofakeit/v6" gofakeit.Name() // Markus Moen gofakeit.Email() // alaynawuckert@kozey.biz gofakeit.Phone() // (570)245-7485 gofakeit.BS() // front-end gofakeit.BeerName() // Duvel gofakeit.Color() // MediumOrchid gofakeit.Company() // Moen, Pagac and Wuckert gofakeit.CreditCardNumber() // 4287271570245748 gofakeit.HackerPhrase() // Connecting the array won't do anything, we need to generate the haptic COM driver! gofakeit.JobTitle() // Director gofakeit.CurrencyShort() // USD
切换Random源
Gofakeit
有多个rand源,默认是math.Rand
,并且使用互斥锁实现并发安全。// Uses crypto/rand(cryptographically secure) with mutext locking faker := gofakeit.NewCrypto() // Pass in your own random source faker := gofakeit.NewCustom()
如果你需要更高的性能,可以手动解除互斥锁,注意这不是并发安全
// Uses math/rand(Pseudo) with NO mutext locking // More performant but not goroutine safe. faker := gofakeit.NewUnlocked(0)
全局设置rand 如果你需要全局替换rand源,只需要简单设置即可:
faker := gofakeit.NewCrypto() gofakeit.SetGlobalFaker(faker)
Struct 生成
Gofakeit
可以为struct生成随机数据,并且可以覆盖所有基本类型,还可以使用tag定制数据import "github.com/brianvoe/gofakeit/v6" // Create structs with random injected data type Foo struct { Str string Int int Pointer *int Name string `fake:"{firstname}"` // Any available function all lowercase