「GoCN酷Go推薦」偽數(shù)據(jù)生成器—— bxcodec/faker
日常開發(fā)中通常需要大批量生成偽數(shù)據(jù),用來測試程序。Go 開發(fā)中通常用 bxcodec/faker 來批量生成測試數(shù)據(jù)。bxcodec/faker 功能齊全,測試充分,支持多種語言(包含中文),足夠滿足日常開發(fā)需求,它支持的數(shù)據(jù)類型如下:- int, int8, int16, int32 & int64 - []int, []int8, []int16, []int32 & []int64 - bool & []bool - string & []string - float32, float64, []float32 &[]float64 - time.Time & []time.Time - 以及各種嵌套的結(jié)構(gòu)體字段
注意??:生成數(shù)據(jù)的結(jié)構(gòu)體字段必須是公開,否則會(huì)引起程序恐慌(異常)。
為生成數(shù)據(jù)而提供的方法如下:
package?main
import?"github.com/bxcodec/faker/v3"
//?單一的假函數(shù)可用于檢索特定的值。
func?Example_singleFakeData()?{
?//?地址
?faker.Latitude()??//?=>?81.12195
?faker.Longitude()?//?=>?-84.38158
?//?時(shí)間
?faker.UnixTime()???//?=>?1197930901
?faker.Date()???????//?=>?1982-02-27
?faker.TimeString()?//?=>?03:10:25
?faker.MonthName()??//?=>?February
?faker.YearString()?//?=>?1994
?faker.DayOfWeek()??//?=>?Sunday
?faker.DayOfMonth()?//?=>?20
?faker.Timestamp()??//?=>?1973-06-21?14:50:46
?faker.Century()????//?=>?IV
?faker.Timezone()???//?=>?Asia/Jakarta
?faker.Timeperiod()?//?=>?PM
?//?網(wǎng)絡(luò)
?faker.Email()??????//?=>[email protected]
?faker.MacAddress()?//?=>?cd:65:e1:d4:76:c6
?faker.DomainName()?//?=>?FWZcaRE.org
?faker.URL()????????//?=>?https://www.oEuqqAY.org/QgqfOhd
?faker.Username()???//?=>?lVxELHS
?faker.IPv4()???????//?=>?99.23.42.63
?faker.IPv6()???????//?=>?975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d
?faker.Password()???//?=>?dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTP
?//?詞匯和句子
?faker.Word()??????//?=>?nesciunt
?faker.Sentence()??//?=>?Consequatur?perferendis?voluptatem?accusantium.
?faker.Paragraph()?//?=>?Aut?consequatur?sit?perferendis?accusantium?voluptatem.?Accusantium?perferendis?consequatur?voluptatem?sit?aut.?Aut?sit?accusantium?consequatur?voluptatem?perferendis.?Perferendis?voluptatem?aut?accusantium?consequatur?sit.
?//?支付
?faker.CCType()?????????????//?=>?American?Express
?faker.CCNumber()???????????//?=>?373641309057568
?faker.Currency()???????????//?=>?USD
?faker.AmountWithCurrency()?//?=>?USD?49257.100
?//?人物
?faker.TitleMale()???????//?=>?Mr.
?faker.TitleFemale()?????//?=>?Mrs.
?faker.FirstName()???????//?=>?Whitney
?faker.FirstNameMale()???//?=>?Kenny
?faker.FirstNameFemale()?//?=>?Jana
?faker.LastName()????????//?=>?Rohan
?faker.Name()????????????//?=>?Mrs.?Casandra?Kiehn
?//?電話號(hào)
?faker.Phonenumber()?????????//?->?201-886-0269
?faker.TollFreePhoneNumber()?//?=>?(777)?831-964572
?faker.E164PhoneNumber()?????//?=>?+724891571063
?//??UUID
?faker.UUIDHyphenated()?//?=>?8f8e4463-9560-4a38-9b0c-ef24481e4e27
?faker.UUIDDigit()??????//?=>?90ea6479fd0e4940af741f0a87596b73
?//?唯一值
?faker.SetGenerateUniqueValues(true)?//?在單一的假數(shù)據(jù)功能上實(shí)現(xiàn)唯一的數(shù)據(jù)生成
?faker.Word()
?faker.SetGenerateUniqueValues(false)?//?禁用單一假數(shù)據(jù)功能上的唯一數(shù)據(jù)生成
?faker.ResetUnique()??????????????????//?用來重置所有產(chǎn)生的唯一值
}
第一步:在項(xiàng)目中安裝
go?get?github.com/bxcodec/faker/v3
第二步:項(xiàng)目中導(dǎo)入它并使用
不帶標(biāo)簽的結(jié)構(gòu)體示例:
package?main
import?(
?"fmt"
?"github.com/bxcodec/faker/v3"
)
type?FirstStruct?struct?{
?Image?string
}
type?SecondStruct?struct?{
?Number????????int64
?Height????????int64
?AnotherStruct?FirstStruct
}
type?SomeStruct?struct?{
?Int??????int
?Int8?????int8
?Int16????int16
?Int32????int32
?Int64????int64
?String???string
?Bool?????bool
?SString??[]string
?SInt?????[]int
?SInt8????[]int8
?SInt16???[]int16
?SInt32???[]int32
?SInt64???[]int64
?SFloat32?[]float32
?SFloat64?[]float64
?SBool????[]bool
?Struct???FirstStruct
}
func?main()?{
?a?:=?SomeStruct{}
?err?:=?faker.FakeData(&a)
?if?err?!=?nil?{
??fmt.Println(err)
?}
?fmt.Printf("%+v",?a)
}
帶標(biāo)簽的結(jié)構(gòu)體示例:
ring??`faker:"title_female"`
?FirstName??????????strinpackage?main
import?(
?"fmt"
?"github.com/bxcodec/faker/v3"
)
type?SomeStructWithTags?struct?{
?Latitude???????????float32?`faker:"lat"`
?Longitude??????????float32?`faker:"long"`
?CreditCardNumber???string??`faker:"cc_number"`
?CreditCardType?????string??`faker:"cc_type"`
?Email??????????????string??`faker:"email"`
?DomainName?????????string??`faker:"domain_name"`
?IPV4???????????????string??`faker:"ipv4"`
?IPV6???????????????string??`faker:"ipv6"`
?Password???????????string??`faker:"password"`
?Jwt????????????????string??`faker:"jwt"`
?PhoneNumber????????string??`faker:"phone_number"`
?MacAddress?????????string??`faker:"mac_address"`
?URL????????????????string??`faker:"url"`
?UserName???????????string??`faker:"username"`
?TollFreeNumber?????string??`faker:"toll_free_number"`
?E164PhoneNumber????string??`faker:"e_164_phone_number"`
?TitleMale??????????string??`faker:"title_male"`
?TitleFemale????????stg??`faker:"first_name"`
?FirstNameMale??????string??`faker:"first_name_male"`
?FirstNameFemale????string??`faker:"first_name_female"`
?LastName???????????string??`faker:"last_name"`
?Name???????????????string??`faker:"name"`
?UnixTime???????????int64???`faker:"unix_time"`
?Date???????????????string??`faker:"date"`
?Time???????????????string??`faker:"time"`
?MonthName??????????string??`faker:"month_name"`
?Year???????????????string??`faker:"year"`
?DayOfWeek??????????string??`faker:"day_of_week"`
?DayOfMonth?????????string??`faker:"day_of_month"`
?Timestamp??????????string??`faker:"timestamp"`
?Century????????????string??`faker:"century"`
?TimeZone???????????string??`faker:"timezone"`
?TimePeriod?????????string??`faker:"time_period"`
?Word???????????????string??`faker:"word"`
?Sentence???????????string??`faker:"sentence"`
?Paragraph??????????string??`faker:"paragraph"`
?Currency???????????string??`faker:"currency"`
?Amount?????????????float64?`faker:"amount"`
?AmountWithCurrency?string??`faker:"amount_with_currency"`
?UUIDHypenated??????string??`faker:"uuid_hyphenated"`
?UUID???????????????string??`faker:"uuid_digit"`
?Skip???????????????string??`faker:"-"`
?PaymentMethod??????string??`faker:"oneof:?cc,?paypal,?check,?money?order"`?//?oneof?will?randomly?pick?one?of?the?comma-separated?values?supplied?in?the?tag
?AccountID??????????int?????`faker:"oneof:?15,?27,?61"`?????????????????????//?use?commas?to?separate?the?values?for?now.?Future?support?for?other?separator?characters?may?be?added
?Price32????????????float32?`faker:"oneof:?4.95,?9.99,?31997.97"`
?Price64????????????float64?`faker:"oneof:?47463.9463525,?993747.95662529,?11131997.978767990"`
?NumS64?????????????int64???`faker:"oneof:?1,?2"`
?NumS32?????????????int32???`faker:"oneof:?-3,?4"`
?NumS16?????????????int16???`faker:"oneof:?-5,?6"`
?NumS8??????????????int8????`faker:"oneof:?7,?-8"`
?NumU64?????????????uint64??`faker:"oneof:?9,?10"`
?NumU32?????????????uint32??`faker:"oneof:?11,?12"`
?NumU16?????????????uint16??`faker:"oneof:?13,?14"`
?NumU8??????????????uint8???`faker:"oneof:?15,?16"`
?NumU???????????????uint????`faker:"oneof:?17,?18"`
}
func?main()?{
?a?:=?SomeStructWithTags{}
?err?:=?faker.FakeData(&a)
?if?err?!=?nil?{
??fmt.Println(err)
?}
?fmt.Printf("%+v",?a)
}
帶標(biāo)簽指定字段長度的結(jié)構(gòu)體示例 :“`go package faker_test
import?(?“fmt”
"github.com/bxcodec/faker/v3"
)
//?你可以為你的隨機(jī)字符串設(shè)置長度,也可以為你的整數(shù)設(shè)置邊界。func main()?{?type?SomeStruct?struct?{?Inta?int?faker:"boundary_start=5,?boundary_end=10"?Int8?int8?faker:"boundary_start=100,?boundary_end=1000"?Int16?int16?faker:"boundary_start=123,?boundary_end=1123"?Int32?int32?faker:"boundary_start=-10,?boundary_end=8123"?Int64?int64?faker:"boundary_start=31,?boundary_end=88"
?UInta??uint???`faker:"boundary_start=35,?boundary_end=152"`
?UInt8??uint8??`faker:"boundary_start=5,?boundary_end=1425"`
?UInt16?uint16?`faker:"boundary_start=245,?boundary_end=2125"`
?UInt32?uint32?`faker:"boundary_start=0,?boundary_end=40"`
?UInt64?uint64?`faker:"boundary_start=14,?boundary_end=50"`
?ASString?[]string??????????`faker:"len=50"`
?SString??string????????????`faker:"len=25"`
?MSString?map[string]string?`faker:"len=30"`
?MIint????map[int]int???????`faker:"boundary_start=5,?boundary_end=10"`
}
_?=?faker.SetRandomMapAndSliceSize(20)?//?隨機(jī)生成的數(shù)據(jù)大小不會(huì)超過20個(gè)...
a?:=?SomeStruct{}
_?=?faker.FakeData(&a)
fmt.Printf("%+v",?a)
}
執(zhí)行程序打印出生成的數(shù)據(jù)。
bxcodec/faker提供的功能不止以上幾種,還提供指定語言格式生成字符串?dāng)?shù)據(jù),使用非常簡單。
bxcodec/faker 基本能滿足常見需求,也有一些可能滿足不了,如它不支持 map[interface{}]interface{},map[any_type]interface{},map[interface{}]any_type數(shù)據(jù)類型;不完全支持自定義類型,目前最穩(wěn)妥的使用方式就是不使用任何自定義類型,以避免引起程序恐慌(異常)。
bxcodec/faker 是很優(yōu)秀的第三方偽造數(shù)據(jù)生成庫,使用簡單,功能齊全,測試充分,維護(hù)者活躍;能滿足日常批量生成測試數(shù)據(jù)的需求,有效提升開發(fā)測試效率,推薦大家使用。
https://github.com/bxcodec/faker https://pkg.go.dev/github.com/bxcodec/faker/v3#section-documentation
《酷Go推薦》招募:
各位Gopher同學(xué),最近我們社區(qū)打算推出一個(gè)類似GoCN每日新聞的新欄目《酷Go推薦》,主要是每周推薦一個(gè)庫或者好的項(xiàng)目,然后寫一點(diǎn)這個(gè)庫使用方法或者優(yōu)點(diǎn)之類的,這樣可以真正的幫助到大家能夠?qū)W習(xí)到
新的庫,并且知道怎么用。
大概規(guī)則和每日新聞?lì)愃?,如果?bào)名人多的話每個(gè)人一個(gè)月輪到一次,歡迎大家報(bào)名!戳「閱讀原文」,即可報(bào)名
掃碼也可以加入 GoCN 的大家族喲~
