From c5280523caa79a5680db680e1534256770c320df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 27 May 2022 00:09:46 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++++++++++++ .gitignore | 9 +++++++++ go.mod | 3 +++ gorandom.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 3 +++ version_test.go | 7 +++++++ 6 files changed, 89 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 gorandom.go create mode 100644 version.go create mode 100644 version_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4794692 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.env +.git +.svn +.idea +.vscode +*.log +goinit.sh +gomod.sh +/vendor/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2a5e8ef --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gorandom + +go 1.18 diff --git a/gorandom.go b/gorandom.go new file mode 100644 index 0000000..34281cc --- /dev/null +++ b/gorandom.go @@ -0,0 +1,50 @@ +package gorandom + +import ( + "math/rand" + "time" +) + +const numbers string = "0123456789" +const letters string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +const specials = "~!@#$%^*()_+-=[]{}|;:,./<>?" +const alphanumerics string = letters + numbers +const ascii string = alphanumerics + specials + +func random[T int | int64](n T, chars string) string { + if n <= 0 { + return "" + } + r := rand.New(rand.NewSource(time.Now().UnixNano())) + bytes := make([]byte, n, n) + l := len(chars) + var i T = 0 + for { + if i >= n { + break + } + bytes[i] = chars[r.Intn(l)] + i++ + } + return string(bytes) +} + +// Alphanumeric 随机字母数字 +func Alphanumeric[T int | int64](n T) string { + return random(n, alphanumerics) +} + +// Alphabetic 随机字母 +func Alphabetic[T int | int64](n T) string { + return random(n, letters) +} + +// Numeric 随机数字 +func Numeric[T int | int64](n T) string { + return random(n, numbers) +} + +// Ascii 随机ASCII +func Ascii[T int | int64](n T) string { + return random(n, ascii) +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..d1b1ddd --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package gorandom + +const Version = "1.0.0" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..2b0146d --- /dev/null +++ b/version_test.go @@ -0,0 +1,7 @@ +package gorandom + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +}