gotool

GoTool is a small and complete Golang tool set. It mainly refines and integrates the commonly used methods in daily development to avoid repeating the wheel and improve work efficiency. Each method is extracted from the author’s work experience and previous projects.

Please see the documentation for detailed use of 2021-7-9 updates

  • Add file IO manipulation tool FileUtils
  • Add CaptchaUtils, a captcha generation tool
  • Add file directory compression and decompression tool ZipUtis
  • String array tool StrarrayUtils

How to use gotool?

The installation

go get github.com/druidcaesa/gotool

go.mod github.com/druidcaesa/gotool

The introduction of

import "github.com/druidcaesa/gotool"

StrUtils

Golang is a string tool set, which covers the most commonly used tools in development. It is currently in incomplete development

1, gotool. StrUtils. ReplacePlaceholder placeholder is replaced

Func TestStringReplacePlaceholder (t * testing. T) {s: = "you are my {}, I am your {}" placeholder, Err: = gotool. StrUtils. ReplacePlaceholder (s, "only", "All") if err = = nil {FMT. Println (placeholder)}} / / out = = = RUN TestStringReplacePlaceholder you are my only, I am your all - PASS: PASS TestStringReplacePlaceholder (0.00 s)

2, gotool. StrUtils. RemoveSuffix remove file extension for the file name

func TestRemoveSuffix(t *testing.T) { fullFilename := "test.txt" suffix, _ := gotool.StrUtils.RemoveSuffix(fullFilename) fmt.Println(suffix) fullFilename = "/root/home/test.txt" suffix, _ = gotool.StrUtils.RemoveSuffix(fullFilename) fmt.Println(suffix) } //out == = RUN TestRemoveSuffix test test --- PASS: PASS TestRemoveSuffix (0.00 s)

3, gotool. StrUtils. GetSuffix for file extension

func TestGetSuffix(t *testing.T) { fullFilename := "test.txt" suffix, _ := gotool.StrUtils.GetSuffix(fullFilename) fmt.Println(suffix) fullFilename = "/root/home/test.txt" suffix, _ = gotool.StrUtils.GetSuffix(fullFilename) fmt.Println(suffix) } //out == = RUN TestGetSuffix .txt .txt --- PASS: PASS TestGetSuffix (0.00 s)

4, GoTool.strutils.hasEmpty determines if the string is not empty. I return true empty

func TestHasStr(t *testing.T) { str := "" empty := gotool.StrUtils.HasEmpty(str) fmt.Println(empty) str = "11111" empty = gotool.StrUtils.HasEmpty(str) fmt.Println(empty) } //out == = RUN TestHasStr true false --- PASS: PASS TestHasStr (0.00 s)

StrarrayUtils string manipulation tool

1, gotool. StrArrayUtils. StringToInt64 string array int64 array, please make sure that before invoking an array of strings are all digital

String testToint64 (t * testing.t) {String testToint64 (t * testing.t) {String testToint64 (t * testing.t) = [] String {"1", "23123", "23123", "232323"} fmt.Println(reflect.TypeOf(strings[0])) toInt64, err := gotool.StrArrayUtils.StringToInt64(strings) if err ! = nil { t.Fatal(err) } fmt.Println(reflect.TypeOf(toInt64[0])) } //out == = RUN TestStringToInt64 string int64 --- PASS: PASS TestStringToInt64 (0.00 s)

2, gotool. StrArrayUtils. StringToInt32 string array int64 array, please make sure that before invoking an array of strings are all digital

String testToint32 (t * testing.t) {String testToint32 (t * testing.t) {String testToint32 (t * testing.t) {String testToint32 (t * testing.t) {String testToint32 (t * testing.t) {String testToint32 (t * testing.t); "232323"} fmt.Println(reflect.TypeOf(strings[0])) toInt64, err := gotool.StrArrayUtils.StringToInt32(strings) if err ! = nil { t.Fatal(err) } fmt.Println(reflect.TypeOf(toInt64[0])) } //out == = RUN TestStringToInt32 string int32 --- PASS: PASS TestStringToInt32 (0.00 s)

3, gotool. StrArrayUtils. ArrayDuplication array to heavy

Func estarrayduplication (t * testing.t) {// String String String String String String String {"hello", "word", "gotool", "Word "} FMT.Println(" -----------------> before reduplication ", Strings) duplication: = gotool. StrArrayUtils. ArrayDuplication (strings) FMT. Println (" go after heavy -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > ", Duplication)} / / out = = = the RUN before TestArrayDuplication to heavy -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > [hello word gotool word] to after heavy -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > [Hello Word gotool] -- Pass: StarRayMesh (0.00s) Pass

DateUtil

Golang is a set of time manipulation tools, which basically covers the most commonly used tools in development, and is currently in incomplete development

1, gotool. DateUtil. FormatToString time formatted into a string

func TestFormatToString(t *testing.T) { now := gotool.DateUtil.Now() toString := gotool.DateUtil.FormatToString(&now, "YYYY-MM-DD hh:mm:ss") fmt.Println(toString) toString = gotool.DateUtil.FormatToString(&now, "YYYYMMDD HHMMSS") FMT. Println (toString)} / / minutes when (date) (month) (year) corresponding to the DD YYYY MM HHMMSS can be any combination Such as YYYY hh YYYY - DD hh: mm / / out = = =, etc TestFormatToString 2021-07-07 16:13:30 20210707 161330 -- -- PASS: TestFormatToString 2021-07-07 16:13:30 20210707 161330 -- PASS

2. GoTool. DateUtil. Iszero determines if the time is empty

T * testing.t) {t2 := time.time {} zero := goTool.dateutil.iszero (t2); fmt.Println(zero) zero = gotool.DateUtil.IsZero(gotool.DateUtil.Now()) fmt.Println(zero) } //out == = RUN TestDate_IsZero true false -- PASS: TestDate_IsZero (0.00s) PASS

GoTool.dateutil.Now gets the current time equal to time.now (). This method is also included in the tool for consistency

4, gotool. DateUtil. InterpretStringToTimestamp type string formatted into time

// Parameter 1 needs to format the time string Parameter 2 string format, Needs and must be formatted string format consistent / / such as the corresponding YYYY - 2021-6-4 - MM DD 2021.6.4 corresponding YYYY. MM. DD func TestInterpretStringToTimestamp (t * testing in t) { timestamp, err := gotool.DateUtil.InterpretStringToTimestamp("2021-05-04 15:12:59", "YYYY-MM-DD hh:mm:ss") if err ! = nil { gotool.Logs.ErrorLog().Println(err.Error()) } fmt.Println(timestamp) } //out == = RUN TestInterpretStringToTimestamp 1620112379 - PASS: TestInterpretStringToTimestamp PASS (0.00 s)

5, gotool. DateUtil. UnixToTime timestamp transfer time

Func TestUnixToTime (t * testing. T) {Unix: = gotool. DateUtil. Now (). The Unix (FMT). The Println (" time stamp -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > ", unix) toTime := gotool.DateUtil.UnixToTime(unix) fmt.Println(toTime) } //out == = RUN TestUnixToTime Time stamp -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > the 16:14:42 + 0800 2021-07-07 1625645682 CST - PASS: TestUnixToTime PASS (0.00 s)

6, gotool. DateUtil. GetWeekDay for week

func TestGetWeekDay(t *testing.T) { now := gotool.DateUtil.Now() day := gotool.DateUtil.GetWeekDay(now) FMT. Println (" today is -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- week, day} / / out = = = RUN TestGetWeekDay today is -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - week 3 - PASS: PASS TestGetWeekDay (0.00 s)

7, gotool. DateUtil. MinuteAddOrSub HourAddOrSub, DayAddOrSub time calculation tool

/ / time calculating func TestTimeAddOrSub (t * testing. T) {now: = gotool. DateUtil. Now) (FMT) Println (" the time is now -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > ", Now) sub: = gotool. DateUtil. MinuteAddOrSub (now, 10) FMT. Println (" minutes results -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub) sub = gotool. DateUtil. MinuteAddOrSub (now - 10) FMT. Println (" minutes results -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub) sub = gotool. DateUtil. HourAddOrSub (now, 10) FMT. Println (" hour calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub) sub = gotool. DateUtil. HourAddOrSub (now - 10) FMT. Println (" hour calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub) sub = gotool. DateUtil. DayAddOrSub (now, 10) FMT. Println (" day calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub) sub = gotool. DateUtil. DayAddOrSub (now - 10) FMT. Println (" day calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Sub)} / / the time is now -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 2021-07-07 11:18:17. 8295757 + 0800 CST m = + / 0.012278001 / minutes results -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > The 2021-07-07 11:28:17. 8295757 + 0800 CST m = + / 600.012278001 / minutes results -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 2021-07-07 11:08:17. 8295757 + 0800 CST m = 599.987721999 / / hour calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 2021-07-07 21:18:17. 8295757 + 0800 CST m = + 36000.012278001 / / hour calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 2021-07-07 01:18:17. 8295757 + 0800 CST m = 35999.987721999 / / day calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > The 2021-07-17 11:18:17. 8295757 + 0800 CST m = + / 864000.012278001 / day calculation result -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 2021-06-27 11:18:17. 8295757 + 0800 CST m = 863999.987721999

Convertutils Gregorian calendar to lunar calendar tool

1, gotool. ConvertUtils. GregorianToLunarCalendar (Gregorian calendar lunar calendar), GetLunarYearDays (lunar calendar), GetLunarYearDays (access to the lunar calendar this year lunar calendar days)

func TestConvertTest(t *testing.T) { calendar := gotool.ConvertUtils.GregorianToLunarCalendar(2020, 2, 1) fmt.Println(calendar) gregorian := gotool.ConvertUtils.LunarToGregorian(calendar[0], calendar[1], calendar[2], false) fmt.Println(gregorian) days := gotool.ConvertUtils.GetLunarYearDays(2021) fmt.Println(days) } //[2020 1 8] / / [1, 2] 2020 / / 354

2, gotool. ConvertUtils. GetSolarMonthDays (2021, 7) to obtain the Gregorian calendar month number of days in July 2021

3, gotool. ConvertUtils. IsLeapYear (2021) to get one is true or false is not in

4, gotool. ConvertUtils. GetLeapMonth get some year leap month month (2021)

BcryptUtils encryption and decryption tools

1, gotool. BcryptUtils. Generate encryption processing, usually for the password will be encrypted in a database storage use, irreversible

2, gotool.BcryptUtils.Com pareHash encrypted and unencrypted password contrast, mostly used for login authentication

Func TestGenerate (t * testing. T) {/ / encrypt the generate: = gotool. BcryptUtils. Generate (" 123456789 ") FMT. Println (generate) / / contrast to encrypt the hash: = gotool.BcryptUtils.Com pareHash (generate, "123456789") fmt.Println(hash) } //out == = RUN TestGenerate $2 a $10 $IACJ6zGuNuzaumrvDz58Q. VJUzz4JGqougYKrdCs48rQYIRjAXcU2 true - PASS: TestGenerate PASS (0.11 s)

3, GoTool.bCryptutils.md5 MD5 encryption

func TestMd5(t *testing.T) { md5 := gotool.BcryptUtils.MD5("123456789") fmt.Println(md5) } //out == = RUN TestMd5 25 f9e794323b453885f5181f1b624d0b - PASS: TestMd5 PASS (0.00 s)

4, gotool. BcryptUtils. GenRsaKey (access to public and private keys),

5, rsasignWithSHA256 (to sign),

6, RSAVERYSIGNWITHSHA256 (verification),

RSAENCRYPT (public key encryption)

8, RsaDecrypt(private key decryption)

Func TestRsa(t * testing.t) {// RSA key file generated FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - to obtain RSA public/private key -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ") prvKey, pubKey := gotool.BcryptUtils.GenRsaKey() fmt.Println(string(prvKey)) fmt.Println(string(pubKey)) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - for signature and verification operation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ") var data = "I was encrypted data to remember I oh -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -" FMT. Println (" signature on the message for operation..." ) signData: = gotool. BcryptUtils. RsaSignWithSha256 (byte [] (data), prvKey) FMT. Println (" the signature of the message information: ", hex. encodeToString (signData)) fmt.println ("\n verifies the signature information...") ) if gotool. BcryptUtils. RsaVerySignWithSha256 (byte [] (data), signData, pubKey) {FMT. Println (" signature information verification is successful, determine the private key signature is correct!!" )} FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - encrypt decrypt operation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ") ciphertext: = Gotool. BcryptUtils. RsaEncrypt (byte [] (data), pubKey) FMT. Println (" public-key encryption of data: ", hex.EncodeToString(ciphertext)) sourceData := gotool.BcryptUtils.RsaDecrypt(ciphertext, PrvKey) fmt.println (" Data decrypted after private key: ", string(sourceData)) } //out == = RUN TestRsa -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - to obtain RSA public/PRIVATE KEY -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- BEGIN RSA PRIVATE KEY -- -- -- -- -- MIICXAIBAAKBgQCgHh1ZYFjlxrIJYjjWGFaLwC8Oov8KqyMtHa+GauF121dperr3 i46JyDoskoSBhbkmqv70LMNrjqVdttdIsC0BtH9ThWLBwKVLH56EqfzqlzClKZEh WTNaddCSuxoZpN33mwS82DCjZe3d7nAPdEGD5pSBx6TVt5bG1c3NVAmcBQIDAQAB AoGAWc5KO9T0R3xYYzb6Fer0r9GNEzKMxdkTE7zws/3Cky4BKyIxN6LIwbLSHinX tCXioTOLaDyrJuqNCbEBsr1NoCIPxnswA5Jm5QDYO5x9aq4u8+SZ9KqLbMrO1JDS ZV7Cbiblz1xNMfdVIvlVjD5RdEotbYTbHI2CZUoPsjHng8kCQQDHi6TJYJqvej8r q46ZceuWHDgE81Wu16RrA/kZKi6MJAApQtfO/4HM6W/ImbCjZ2rSYxqnAlIg/GxA dT6iJABjAkEAzWra06RyhGm3lk9j9Xxc0YPX6VX7qT5Iq6c7ry1JtTSeVOksKANG elaNnCj8YYUgj7BeBBcMMvLd39hP1h06dwJAINTyHQwfB2ZGxImqocajq4QjF3Vu EKF8dPsnXiOZmwdFW4Sa+30Av1VdRhU7gfc/FTSnKvlvx+ugaA6iao0f3wJBALa8 sTCH4WwUE8K+m4DeAkBMVn34BKnZg5JYcgrzcdemmJeW2rY5u6/HYbCi8WnboUzS K8Dds/d7AJBKgTNLyx8CQBAeU0St3Vk6SJ6H71J8YtVxlRGDjS2fE0JfUBrpI/bg r/QI8yMAMyFkt1YshN+UNWJcvR5SXQnyT/udnWJIdg4 = -----END RSA PRIVATE KEY----- -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgHh1ZYFjlxrIJYjjWGFaLwC8O ov8KqyMtHa+GauF121dperr3i46JyDoskoSBhbkmqv70LMNrjqVdttdIsC0BtH9T hWLBwKVLH56EqfzqlzClKZEhWTNaddCSuxoZpN33mwS82DCjZe3d7nAPdEGD5pSB x6TVt5bG1c3NVAmcBQIDAQAB -----END PUBLIC KEY----- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - for signature and verification operation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the message signature operation... Message signature information:  1fcf20c4fb548c8fc0906e369287feb84c861bf488d822d78a0eada23d1af66ed3a12e9440d7181b1748fd0ad805222cf2ce7ce1f6772b330ef11b7 17700ba26945dda9d749a5c4d8c108ede103c17bed92801a4c3fbc1ebf38d10bf4bf183713eeb7f429acc3dcc3812366a324737f756720f3f9e75ddd A1E024A7698B89163 Verify the signature information... The signature information has been verified successfully and confirmed to be the correct private key signature!! -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - encrypt decrypt operation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - after a public key encryption of data:  637b05798c1cf95cfcc63adf228645c77f8e9a9f34b12b722e6938ded8c9560a0430171a4f940d3fb2d96bc6c470c80a817d81f4a2669b991adbff5 b22b335129e514c921083ce3e64c1c876409d9b763d5e8e269797283ef951a364da6a59a1d8454391356cb0cd0808092e9dd7ac371f9247a43760f3c After 82 b7ad26a32a7a807 decrypted data: I was the encrypted data to remember me oh -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- PASS: TestRsa PASS (0.02 s)

ZipUtils

Compression and decompression tools, single file compression can also be compressed for directory, or cross directory compression

1, press at gotool.ZipUtils.Com

  • The files array can be multi-directory files
  • The dest compressed file is stored in the address
func TestCompress(t *testing.T) { open, err := os.Open("/Users/fanyanan/Downloads/gotool") if err ! = nil { t.Fatal(err) } files := []*os.File{open} flag, err := gotool.ZipUtils.Compress(files, "/Users/fanyanan/Downloads/test.zip") if err ! = nil {t.atal (err)} if Flag {fmt.println (" Compress successfully ")}}} //out == = RUN TestCompress successfully -- PASS: PASS TestCompress (0.12 s)

2, gotool. ZipUtils. DeCompress

  • Zipfile compressed package path
  • The path to decompress dest
func TestDeCompress(t *testing.T) { compress, err := gotool.ZipUtils.DeCompress("/Users/fanyanan/Downloads/test.zip", "/Users/fanyanan/Downloads") if err ! = nil {t.atal (err)} if compress {fmt.println (" Decompress ")}} //out == = RUN TestDecompress -- PASS: PASS TestDeCompress (0.44 s)

FileUtils

File manipulation tools, make IO operations more simple and convenient, so that IO operation is not so difficult

1. GoTool. fileUtils.exists determines whether a file or directory Exists

Func testFileExists (t * testing.t) {// Check whether a file or directory exists := goTool.fileUtils.exists ("F:/go-test/test") FMT. Println (" create before -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", the exists) err: = OS. MkdirAll (" F: / go - the test/test ", OS. ModePerm) if err! = nil {t.F atal (err)} the exists. = gotool FileUtils. The exists (" F: / go - the test/test "). FMT Println (" create -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Exists)} / / out = = = RUN TestFileExists created before -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > false after creation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > true - PASS: PASS TestFileExists (0.00 s)

2. GoTool. fileUtils.isdir (

Func testsIr (t * testing.t) {// testsIr := goTool.fileUtils.isIr ("F:/go-test/test"); FMT.Println(" Is it directory --------------------->", Dir) dir = gotool. FileUtils. IsDir (" F: / go - the test/test/test. TXT ") FMT. Println (" whether directory -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > ", Dir)} / / out = = = RUN TestIsDir whether directory -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > true whether directory -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > false PASS: PASS TestIsDir (0.00 s)

3, GoTool.FileUtils.isFile

Func TestIsFile(t * test. t) {file := goTool.fileUtils.isfile ("F:/go-test/test"); Fmt.println (" Is it a file --------------------->", File) file = gotool. FileUtils. IsFile (" F: / go - the test/test/test. TXT ") FMT. Println (" whether file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > ", File)} / / out = = = RUN TestIsFile whether file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > false whether file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > true - PASS: PASS TestIsFile (0.00 s)

4, gotool. FileUtils. RemoveFile to delete a file or directory

Func TestRemove (t * testing. T) {/ / delete file file, err: = gotool. FileUtils. RemoveFile (" F: / go - the test/test/test. TXT ") if err! = nil {t.faatal (err)} if file {// Check file exists := goTool.fileUtils.exists ("F:/go-test/test/test.txt") FMT.Println(" Does the file exist ------------------------>", The exists)}} / / out = = = RUN TestRemove file exists -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > false PASS: TestRemove PASS (0.00 s)

5, gotool. FileUtils. OpenFileWronly only write mode to open the file, not automatically created, written from the test

Func testOpenFileWrOnly (t * testing.t) {// Open a file in write only mode, Path := "F:/go-test/test/test.txt" STR := "hello word gotool \n" wronly, if the file does not exist err := gotool.FileUtils.OpenFileWronly(path) if err ! = nil { t.Fatal(err) } defer wronly.Close() write := bufio.NewWriter(wronly) for i := 0; i < 5; I++ {write.writeString (STR)} //Flush the cached file actually writes to the file write.Flush() // read the file to the console files, err := gotool.FileUtils.OpenFileRdonly(path) if err ! = nil { t.Fatal(err) } defer files.Close() reader := bufio.NewReader(files) for { str, err := reader.ReadString('\n') if err ! = nil { break } fmt.Print(str) } } //out == = RUN TestOpenFileWronly hello word gotool hello word gotool hello word Gotool hello word gotool hello word gotool -- PASS: testOpenFileWrOnly (0.00s) PASS

6, gotool. FileUtils. OpenFileRdonly read-only mode to open the file, read the content of the output to the console

func TestOpenFileRdonly(t *testing.T) { path := "F:/go-test/test/test.txt" files, err := gotool.FileUtils.OpenFileRdonly(path) if err ! = nil { t.Fatal(err) } defer files.Close() reader := bufio.NewReader(files) for { str, err := reader.ReadString('\n') if err ! = nil { break } fmt.Print(str) } } //out == = RUN TestOpenFileRdonly hello word gotool hello word gotool hello word Gotool hello word gotool hello word gotool -- PASS: testOpenFilerdOnly (0.00s) PASS

7, gotool. FileUtils. OpenFileAppend open additional content files in the file behind, no will automatically create the file

Func testOpenFileAppend (t * testing.t) {path := "F:/go-test/test/test.txt" STR := "append content \n" wronly, err := gotool.FileUtils.OpenFileAppend(path) if err ! = nil { t.Fatal(err) } defer wronly.Close() write := bufio.NewWriter(wronly) for i := 0; i < 5; I++ {write.writeString (STR)} //Flush the cached file actually writes to the file write.Flush() // read the file to the console files, err := gotool.FileUtils.OpenFileRdonly(path) if err ! = nil { t.Fatal(err) } defer files.Close() reader := bufio.NewReader(files) for { str, err := reader.ReadString('\n') if err ! = nil { break } fmt.Print(str) } } //out == = RUN TestOpenFileAppend hello word gotool hello word gotool hello word Gotool hello word gotool hello word gotool append content append content append content -- PASS: testOpenFileAppend (0.00s) PASS

8, gotool. FileUtils. FileCopy file copying method

Func testFileCopy (t * test.t) {"F:/go-test/test/test.txt"; / / copy money exists: = gotool FileUtils. The exists (copyPath) FMT. Println (" before the copy file exists -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", the exists) fileCopy after / / copy, err := gotool.FileUtils.FileCopy(path, copyPath) if err ! = nil { t.Fatal(err) } if fileCopy { exists := gotool.FileUtils.Exists(copyPath) FMT.Println(" Does the file exist before copying ------------------>", Exists)}} / / out = = = the RUN before TestFileCopy copy file exists -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > false before copy file exists -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > true - PASS: PASS TestFileCopy (0.00 s)

CaptChautils Captcha code generation tool

1, gotool. CaptchaUtils. GetRandStr generated captcha STRNG string

Func testCaptcha (t * testing.t) {// Generate the verification code string, Storage or redis, etc., are used to verify the logical STR: = gotool. CaptchaUtils. GetRandStr (6) FMT. Println (STR)} / / out = = = RUN TestCaptcha Generated captcha string -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > qK5DME - PASS: TestCaptcha PASS (0.01 s)

2, gotool. CaptchaUtils. ImgText generated images [] byte array, can be converted into base64 or image file

func TestCaptcha(t *testing.T) {
str := gotool.CaptchaUtils.GetRandStr(6)
fmt.Println("生成验证码字符串------------------->", str)
//生成图片byte数据,可以根据需要转成base64或者是image图片文件
text := gotool.CaptchaUtils.ImgText(100, 40, str)
sourcestring := base64.StdEncoding.EncodeToString(text)
fmt.Println(sourcestring)
}
//out
== = RUN   TestCaptcha
生成验证码字符串-------------------> qK5DME
iVBORw0KGgo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STSNeLZBwRYekxLHx88q0GNS4/jX90B8fT607b3PhK7mDmx404ylV2jBoZc+Guv3K+jokVIy7JOLRkG4s3AzffWt50SMMITY7DwlKZOdrTuNK8+UB2bz33f8Sh6VPZ7lZI75VpbHNB1xVQNiloYVTpp2++mJ3/aIFWolsozizDFP06hI+OTcHGaRzIE2ySXXo8jotY9fFzbu/Zzevfwu5XxB+X1JXfkZ8vGL74nFhcdI9sRR+n+//phfEC99BqCj3YU/+XAbHdFR0qaWenzg8C6yaes6wQhPBpOCQEFjhpuv/7mydNEKdSjm9feUrhKFHH7CLal9HLJXWbW0pUEqAB+yrzaUnK6rxI+99oQUYQ/nD/z4fiXaMTQTGql0d7rwi//xtOnMydN44apr1WX/ebP8t97xeLxoeKdOzoktOIRHRkSDIEi87PRJ4vf7UHRUHP/B3B9qP1v5S8Wo0P9/S1AU4dGTJZ1pCJKuMmuSDfOhtnYvJeH2MH7NrCV6Z3cnevSPj0uSIEJuWs6IxhhKDFrccrYJy14/WrDyGvU8Ff4mEUVh5Iet3d1d6KEn7zAZFDc8PIpNK5it3/nTB/8m+v8MUt/agJ94/SlJYxo88pOH5ZS45O/sLa62LiyaRG7kkeE8P2xAdF1DRoh6+Kk7TUdPFBFHaASfd8XV6jWLVmjhjsjvxc3/UWT9jg3Cq+v+KNx29Up1+fwbvgVB+fby/wIAAP//mC6VL2hDN4cAAAAASUVORK5CYII=
--- PASS: TestCaptcha (0.01s)
PASS

Logs log printing tool

1, GoTool.Logs.ErrorLog

2. GoTool.Logs.Infolog Loads Log

3. GoTool.Logs.DebugLog

Func TestLogs(t * testing.t) {goTool.logs.errorLog ().println ("Error log test ") goTool.logs.infolog ().println ("Info log test "); Println("Debug log test ")} //out == = RUN TestLogs [ERROR] 2021/07/07 15:58:10logs_test. go:9: Error Log Test [INFO] 2021/07/07 15:58:10 logs_test.go:10: DEBUG] 2021/07/07 15:58:10 logs_test.go:11: TestLogs (0.00s) PASS: TestLogs (0.00s) PASS

PageUtils paging tool

1, gotool. PageUtils. Paginator rainbow paging

func TestPage(t *testing.T) { paginator := gotool.PageUtils.Paginator(5, 20, 500) fmt.Println(paginator) } //out == = RUN TestPage map[AfterPage:6 beforePage:4 currPage:5 pages:[3 4 5 6 7] BeforePages Before Page CurrPages Total number of pages of totalPages

IdUtils

ID generation tool, can generate string ID and int type ID, according to the need to choose their own need to generate rules

1, gotool. IdUtils. IdUUIDToTime according to the rules of time to generate the UUID, into the elimination and true “-” false retain a “-“

func TestUUID(t *testing.T) { time, Err: = gotool. IdUtils. IdUUIDToTime (true) if err = = nil {FMT. Println (" UUID removal was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > ", time) } time, Err = gotool. IdUtils. IdUUIDToTime (false) if err = = nil {FMT. Println (" not remove was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > ", Time)}} / / out = = = RUN TestUUID UUID removal was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > 6 fb94fe4dfd511ebbc4418c04d462680 Not remove was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > 6 fb9c783 - eb - bc44 dfd5 11 to 18 c04d462680 - PASS: TestUUID PASS (0.00 s)

2, gotool. IdUtils. IdUUIDToRan according to random Numbers generated by the UUID is recommended to use this method, and there will not be repeated, and eliminate “-” true or false retain a “-“

time, Err: = gotool. IdUtils. IdUUIDToTime (true) if err = = nil {FMT. Println (" UUID removal was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > ", time) } time, Err = gotool. IdUtils. IdUUIDToTime (false) if err = = nil {FMT. Println (" not remove was generated according to time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > ", Time)} / / out = = = RUN TestUUID UUID removal was generated according to random number -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > cf5bcdc585454cda95447aae186d14e6 According to the random number generation don't remove the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > '-' -- -- -- -- -- > 72035 dba - d45f - 480 - f - b1fd - 508 d1e036f71 - PASS: TestUUID PASS (0.00 s)

3, gotool. IdUtils. CreateCaptcha generated random number id, type int, into the int refs 1 to 18, after more than 18 will cause more than the length of the int

func TestCreateCaptcha(t *testing.T) { captcha, err := gotool.IdUtils.CreateCaptcha(18) if err ! = nil {FMT. Println (err)} FMT. Println (18 "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - >", captcha) captcha, err = gotool.IdUtils.CreateCaptcha(10) if err ! = nil {FMT. Println (err)} FMT. Println (" 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > ", Captcha)} / / out = = = RUN TestCreateCaptcha 18 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - > 492457482855750014 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > 2855750014 - PASS: TestCreateCaptcha PASS (0.00 s)

4, GOTOOL.IDUTILS.GETIDWORK is calculated according to the timestamp to obtain the ID length of INT64 16 bits

Func testGeTidWork (t * testing.t) {work := goTool.idutils.geTidWork () fmt.println (" int64 type ID -------->"); } //out == = RUN testgetidWork (int64) --------> 1625746675366450 -- PASS: PASS TestGetIdWork (0.00 s)

HttpUtils

A simple “HTTP request” package for Golang is GET POST DELETE PUT

How do we use HTTPUTILS?

Resp, err := goTool.httputils.get ("http://127.0.0.1:8000") resp, err := goTool.httputils.get ("http://127.0.0.1:8000") resp, Err: = gotool. HttpUtils. SetTimeout (5) Get (" http://127.0.0.1:8000 "), resp Debug(true).setHeaders (map[string]string{}).get ("http://127.0.0.1:8000") OR req := gotool.HttpUtils.NewRequest() req := gotool.HttpUtils.NewRequest().Debug(true).SetTimeout(5) resp, Err: = the req. Get (" http://127.0.0.1:8000 ") resp, err: = the req. Get (" http://127.0.0.1:8000 ", nil) resp, Err: = the req. Get (" http://127.0.0.1:8000? Id =10&title=HttpRequest") resp, err := req.Get("http://127.0.0.1:8000? id=10&title=HttpRequest", "address=beijing")

Set the request header

req.SetHeaders(map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "keep-alive",
})

req.SetHeaders(map[string]string{
"Source":"api",
})

Set Cookies

req.SetCookies(map[string]string{
"name":"json",
"token":"",
})

OR

gotool.HttpUtils.SetCookies(map[string]string{
"age":"19",
}).Post()

Set the timeout time

req.SetTimeout(5) //default 30s

Object-oriented mode of operation

req := gotool.HttpUtils.NewRequest(). Debug(true). SetHeaders(map[string]string{ "Content-Type": "application/x-www-form-urlencoded", }).SetTimeout(5) resp, Err: = the req. Get (" http://127.0.0.1 ") resp, err: = gotool. HttpUtils. The NewRequest () Get (" http://127.0.0.1 ")

GET

Query parameters

Resp, err: = the req. Get (" http://127.0.0.1:8000 ") resp, err: = the req. Get (" http://127.0.0.1:8000 ", nil) resp, Err: = the req. Get (" http://127.0.0.1:8000? Id =10&title=HttpRequest") resp, err := req.Get("http://127.0.0.1:8000? Id =10&title= httpRequest ", "address= Beijing ") OR resp, err := goTool.httputils.get ("http://127.0.0.1:8000") resp, Err: = gotool. HttpUtils. Debug (true). SetHeaders (map [string] string {}), Get (" http://127.0.0.1:8000 ")

multi-parameter

Resp, err: = the req. Get (" http://127.0.0.1:8000? id=10&title=HttpRequest", map[string]interface{}{ "name": "jason", "score": 100, }) defer resp.Close() body, err := resp.Body() if err ! = nil { return } return string(body)

POST

// Send nil resp, err := goTool.httputils.post ("http://127.0.0.1:8000") // Send integer resp, Err := goTool.httputils.post ("http://127.0.0.1:8000", 100) // Send []byte resp, Err := goTool.httptils.post ("http://127.0.0.1:8000", []byte("bytes data")) // Send io.reader resp, Err := goTool.httputils.post ("http://127.0.0.1:8000", Bytes.newReader (buf []byte)) resp, Err := goTool.httputils.post ("http://127.0.0.1:8000", Strings.NewReader(" String data")) resp, Err := goTool.httputils.post ("http://127.0.0.1:8000", Bytes.newBuffer (buf []byte)) // Send string resp, Err := goTool.httputils.post ("http://127.0.0.1:8000", "title=github&type=1") // Send JSON resp, Err: = gotool. HttpUtils. JSON (), Post (" http://127.0.0.1:8000 ", "{\"id\":10,\"title\":\"HttpRequest\"}") // Send map[string]interface{}{} resp, Err: = the req. Post (" http://127.0.0.1:8000 ", the map [string] interface {} {" id ": 10," title ": "HttpRequest", }) defer resp.Close() body, err := resp.Body() if err ! = nil {return} return string(body) resp, err := goTool.httputils.post ("http://127.0.0.1:8000") resp, JSON("http://127.0.0.1:8000", map[string]interface{}{"title":"github"}) resp, err := Gotool. HttpUtils. Debug (true). SetHeaders (map [string] string {}). The JSON (), Post (" http://127.0.0.1:8000 ", "{\" title \ ": \" making \}" ")

The proxy pattern

proxy, err := url.Parse("http://proxyip:proxyport") if err ! = nil { log.Println(err) } resp, Err := goTool.httputils.proxy (http.proxyURL (Proxy)).get ("http://127.0.0.1:8000/ip") defer resp.close () if err! = nil {log.Println("Request error: %v", err.Error())} body, err := resp.body () if err! = nil {log.Println("Get body error: %v", err. error ())} log.Println(String (body)))

Upload a file

Params: url, filename, fileinput

Resp, err: = the req. Upload (" http://127.0.0.1:8000/upload ", "/ root/demo. TXT", "uploadFile") body, err := resp.Body() defer resp.Close() if err ! = nil { return } return string(body)

Alert mode

The default false

req.Debug(true)

To print as standard output:

[HttpRequest] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Request: GET HTTP: / / 127.0.0.1:8000? name=iceview&age=19&score=100 Headers: map[Content-Type:application/x-www-form-urlencoded] Cookies: map[] Timeout: 30s ReqBody: map[age:19 score:100] -------------------------------------------------------------------

Json

Post JSON request

Set the request header

 req.SetHeaders(map[string]string{"Content-Type": "application/json"})

Or

The req. JSON (). Post (" http://127.0.0.1:8000 ", the map [string] interface {} {" id ": 10," title ": "Making",}) the req. JSON (), Post (" http://127.0.0.1:8000 ", "{\" title \ ": \" making \ ", \ \ "id" : 10} ")

A Post request

Resp, err := req.Post("http://127.0.0.1:8000", map[string]interface{}{"id": 10, "title": "httpRequest ",})

Print formatted JSON

str, err := resp.Export() if err ! = nil { return }

Unmarshall JSON

var u User err := resp.Json(&u) if err ! = nil { return err } var m map[string]interface{} err := resp.Json(&m) if err ! = nil { return err }