gofaker

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

go-faker

Inspired by faker, this is a faker library in go.

Changelog

For details, read version.go

How to use

0.what is Faker?
type Faker struct {
	Locale           string
	Args             []float64 // weight
	cachedGenerators map[string]interface{}
	inited           bool
	Gender           bool
}
// `Args` are a float array which stands for weights
// when geerating, The larger the weight, the easier 
// the corresponding value will appear

// cachedGenerators are those cached generators. once
// inited, will be used next time

1.Generate a random profile:
import (
	"fmt"

	g "github.com/guchengxi1994/go_faker"
)

func main() {
	f := g.Faker{
		Locale: "zh_CN",
		Gender: true,
	}
    // function `Profile` needs a parameter `useCache`,
    // which will use cached generators if useCache==true
	fmt.Printf("f.Profile(true): %v\n", f.Profile(true))
}


/* results
f.Profile(true): 
                        姓名:扈帆, Fan Hu
                        性别:男
                        年龄:44
                        手机号码:17339607017
                        身份证号码:440201197702154954
                        家庭住址:宁夏回族自治区佛山市沈北新区兰州街819座
                        学习经历:1983-9~1989-7,黄浦实验小学,学生
                                  1989-9~1992-7,滨城第二中学,学生
                                  1992-9~1995-7,怀柔纺织学院,学生

                        工作经历:2020-04~Now,彩虹网络有限公司,首席执行官CEO/总裁/总经理
                                  2018-01~2020-04,东方峻景信息有限公司,产品专员
                                  2016-10~2018-01,佳禾传媒有限公司,德语翻译
                                  2012-07~2016-10,超艺科技有限公司,医药代表
                                  2011-04~2012-07,中建创业传媒有限公司,网店/淘宝客服
                                  2005-07~2011-04,通际名联传媒有限公司,行政总监
                                  2003-02~2005-07,通际名联科技有限公司,调色员
                                  2000-09~2003-02,华成育卓网络有限公司,图书管理员/资料管理员
                                  1998-04~2000-09,创亿信息有限公司,电气/电器工程师
                                  1995-10~1998-04,东方峻景科技有限公司,调墨技师

                        简介:我具有良好的英语水平,从大一到大三都有专门的外教授课。在大二时就已经透过国家英语四级和六级,成绩分别是523和499。在大三时,参加雅思考试并取得5。5成绩
*/
2.Generate a random name:

func main() {
    f := g.Faker{
        Locale: "zh_CN",
        Gender: true,  //true is male and false is female
    }
    fmt.Printf("f.PersonName(): %v\n", f.PersonName())
}
3.Generate a random companyname
func main() {
    f := g.Faker{
        Locale: "zh_CN",
    }
    fmt.Printf("f.CompanyName(): %v\n", f.CompanyName())
}
4.Generate a random IPv4 address(with or without port)
func main() {
	f := g.Faker{}
	fmt.Printf("f.IpAddr(true): %v\n", f.IpAddr(true))
}
5.Generate a random job
func main() {
	f := g.Faker{
		Locale: "zh_CN",
	}
	fmt.Printf("f.Job(): %v\n", f.Job())
}
6.Generate a random lorem
func main() {
	f := g.Faker{
		Locale: "zh_CN",
	}
    // parameter uclike is just a joke
	fmt.Printf("f.Lorem(true): %v\n", f.Lorem(true))
}

7.Generate a random mobile number
func main() {
	f := g.Faker{
		Locale: "zh_CN",
	}
	fmt.Printf("f.Mobile(): %v\n", f.Mobile())
}
8.Generate a random nickname
func main() {
	f := g.Faker{
		Gender: true,
	}
	fmt.Printf("f.Nickname(): %v\n", f.Nickname())
}
9.Generate a random SSN
func main() {
	f := g.Faker{
		Gender: true,
		Locale: "zh_CN",
	}
	fmt.Printf("f.SSN(): %v\n", f.SSN())
}
10.Generate a random school name
func main() {
	f := g.Faker{
		Locale: "zh_CN",
	}
	fmt.Printf("f.School(): %v\n", f.School())
}
11.Generate a random isbn (10 and 13)
func main() {
	i := g.ISBN{}
	i.Generate()
	fmt.Printf("i.ToString(): %v\n", i.ToString())
}
12.Others are under construction.

Custom generators

Yes, it is supported after v0.2.1. Maybe not convient right now.
// first, write a custom function
// must  return a string
func testFunc() string {
	return "hahaha"
}

// write a format, read providers/formats.go for details
runStr = `{func.testFunc}`   // format

// add this function to global_functions
provider.AddGlobalFunction("whatever a name",testFunc)

// call this runStr
fmt.Printf("provider.Format(runStr, false): %v\n", provider.Format(runStr, false))
// then, you will get provider.Format(runStr, false): hahaha 

Why a new repo?

I have been learning golang since Sep.2021, it is quite different from python/java/dart. It is not hard but a little not convient when coding such as find out wether an array contains a specific item, or get a substring. And the path module in go maybe not as good as python's os.path module. Also, no try...catch.... I think I need more execises in golang.
And yes, building wheels with a unfamiliar language is amazing.

Documentation

Overview

* @Descripttion: * @version: * @Author: xiaoshuyui * @email: guchengxi1994@qq.com * @Date: 2021-11-26 19:57:46 * @LastEditors: xiaoshuyui * @LastEditTime: 2021-11-26 20:23:24

Index

Constants

This section is empty.

Variables

View Source
var Version = "0.2.1+1"

v0.0.1 zh_CN address generator

v0.0.1+4 random alg

v0.0.2 ipv4 address generator

v0.0.2+1 location generator

v0.0.2+2 mobile(zh_CN) generator

v0.0.2+3 ssn(zh_CN) generator

v0.0.3 profile generator

v0.0.3+1 name generator

v0.0.4 education generator

v0.0.5 add education to profile

v0.0.5+1 update utils/Randn function

v0.0.6 add nickname and lorem(for fun) generators

v0.1.0 Faker

v0.1.1 car license generator

v0.1.2 isbn generator

v0.2.0 create a pattern to generate random

information, such as python faker

v0.2.1 add 2 functions AddGlobalVariants and AddGlobalFunction

   which allows to use custom formats
   eg.
   func testFunc() string {
		return "hahaha"
	}

	runStr = `{func.testFunc}`   // format
	fmt.Printf("provider.Format(runStr, false): %v\n", provider.Format(runStr, false))  // provider.Format(runStr, false): hahaha

v0.2.1+1 fix some errors

Functions

func GenerateIpV4Address

func GenerateIpV4Address(containDNSs bool) string

func GeneratePort

func GeneratePort(max int) string

func GenerateRandomDate

func GenerateRandomDate(max, min int) (string, int)

func IsIpV4Valid

func IsIpV4Valid(ip string, containDNSs bool) bool

Types

type Address

type Address struct {
	PostcodeLength int
	Args           []float64 // weight
	UseWeighting   bool
	Locole         string
}

func (*Address) Address

func (addr *Address) Address(args ...float64) string

generate

func (*Address) BuildingNumber

func (addr *Address) BuildingNumber(length int) string

func (*Address) City

func (addr *Address) City(args ...float64) string

func (*Address) District

func (addr *Address) District(args ...float64) string

func (*Address) Province

func (addr *Address) Province(args ...float64) string

func (*Address) StreetAddress

func (addr *Address) StreetAddress(length int, args ...float64) string

func (*Address) StreetName

func (addr *Address) StreetName(args ...float64) string

type Carlicense added in v0.2.1

type Carlicense struct {
	Locale string
}

func (*Carlicense) Generate added in v0.2.1

func (c *Carlicense) Generate() string

type Company

type Company struct {
	Locale string
	Args   []float64 // weight
}

func (*Company) Generate

func (company *Company) Generate() string

type Experience

type Experience struct {
	StartTime   string `json:"startTime"`
	EndTime     string `json:"endTime"`
	Institution string `json:"institution"`
	Content     string `json:"content"`
}

func GenerateSchoolExperience

func GenerateSchoolExperience(grades int, currentAge int, locale string) ([]Experience, int)

grade > 0 primary school 6 years,

grade = 1 middle school 3 years,

grade = 2 high school 3 years,

grade = 3 university bachelor 4 years,

grade = 4 university master 3 years,

grade = 5 university doctor 4 years,

grade = 6 others 3 years 中专

grade = 7 others 5 years 大专

September to July

studyStartAge = one of [5,6,7,8]

func GenerateWorkExperiences

func GenerateWorkExperiences(times, years int, locale string) []Experience

times is the number join work

years is the number form when to now

func (*Experience) ToString

func (e *Experience) ToString() string

type Faker added in v0.1.0

type Faker struct {
	Locale string
	Args   []float64 // weight

	Gender bool
	// contains filtered or unexported fields
}

address generator

company(name) generator

experience_generator

ipaddress generator

job generator

lorem generator

mobile generator

nickname generator

personname generator

profile generator

school generator

ssn generator

func (*Faker) CompanyName added in v0.1.0

func (f *Faker) CompanyName() string

generate a random company name

func (*Faker) Init added in v0.1.0

func (f *Faker) Init()

func (*Faker) IpAddr added in v0.1.0

func (f *Faker) IpAddr(withport bool) string

generate a random ip address

func (*Faker) Job added in v0.1.0

func (f *Faker) Job() string

generate a random job

func (*Faker) Lorem added in v0.1.0

func (f *Faker) Lorem(uclike bool) string

generate a random lorem, uclike is just for fun

func (*Faker) Mobile added in v0.1.0

func (f *Faker) Mobile() string

generate a random mobile

func (*Faker) Nickname added in v0.1.0

func (f *Faker) Nickname() string

generate a random nickname

func (*Faker) PersonName added in v0.1.0

func (f *Faker) PersonName() string

generate a random name with gender(bool)

func (*Faker) Profile added in v0.1.0

func (f *Faker) Profile(useCache bool) string

generate a random profile

useCache(true) if related generators are used

func (*Faker) SSN added in v0.1.0

func (f *Faker) SSN() string

func (*Faker) School added in v0.1.0

func (f *Faker) School() string

generate a random school name

type Generators added in v0.1.0

type Generators struct {
	Gpname   *PersonName
	Gssn     *SSN
	Gmobile  *Mobile
	Gaddress *Address
}

type ISBN added in v0.2.1

type ISBN struct {
	ISBN13 string
	ISBN10 string
}

func (*ISBN) Generate added in v0.2.1

func (isbn *ISBN) Generate()

func (*ISBN) ToString added in v0.2.1

func (isbn *ISBN) ToString() string

type Job

type Job struct {
	Locale string
}

func (*Job) Generate

func (job *Job) Generate() string

type Location

type Location struct {
	Longitude string // 经度  0-180
	Latitude  string // 维度  0-90
	WE        string
	NS        string
}

func (*Location) GetPositon

func (location *Location) GetPositon() string

type Lorem added in v0.1.0

type Lorem struct {
	LikeUC    bool // only supports chinese
	Locale    string
	Type      int // 0 for paragraph,1 for short sentence(titles)
	MaxLength int // length for each sentence

	Content string //generated string
	// contains filtered or unexported fields
}

func (*Lorem) Generate added in v0.1.0

func (lorem *Lorem) Generate()

func (*Lorem) ToString added in v0.1.0

func (lorem *Lorem) ToString() string

type Mobile

type Mobile struct {
	Locale   string
	City     string
	Supplier string
}

func (*Mobile) Generate

func (mobile *Mobile) Generate() string

type Nickname added in v0.1.0

type Nickname struct {
	Prefix string
	Suffix string
	Gender bool
	// contains filtered or unexported fields
}

func (*Nickname) Generate added in v0.1.0

func (nickname *Nickname) Generate()

func (*Nickname) ToString added in v0.1.0

func (nickname *Nickname) ToString() string

type PersonName

type PersonName struct {
	Locale    string
	Firstname string
	Lastname  string
	Gender    bool
	Pinyin    string //only for chinese names
	// contains filtered or unexported fields
}

func (*PersonName) Generate

func (p *PersonName) Generate(args ...float64)

func (*PersonName) ToString

func (p *PersonName) ToString(withPinyin bool) string

type Profile

type Profile struct {
	Name            string `json:"name"`
	Gender          bool   `json:"gender"`
	IdNumber        string `json:"idNumber"`
	MobileNumber    string `json:"mobile"`
	Locale          string
	HomeAddress     string       `json:"homeAddress"`
	WorkExperiences []Experience `json:"workExperiences"`
	Description     string       `json:"description"`

	Age                  int          `json:"age"`
	EducationExperiences []Experience `json:"educationExperiences"` // 至少初中毕业,高中毕业,大学毕业,硕士,博士,大专
	// contains filtered or unexported fields
}

func (*Profile) FormatExperice

func (p *Profile) FormatExperice(t int) string

func (*Profile) Generate

func (p *Profile) Generate(generators *Generators)

func (Profile) ToString

func (p Profile) ToString() string

type SSN

type SSN struct {
	Locale string
	MaxAge int
	MinAge int
	Gender bool
	// contains filtered or unexported fields
}

func (*SSN) Generate

func (ssn *SSN) Generate() string

func (*SSN) GenerateSequenceCode

func (ssn *SSN) GenerateSequenceCode() string

type School

type School struct {
	Type   int // 0 primary school ,1 middle school, 2 university 3 职业技术学院
	Locale string
}

func (*School) Generate

func (s *School) Generate() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL