commit e5df799c8cebc2ddf13a58888f7a4e73b6139ba3 Author: sre Date: Fri Jun 17 11:15:19 2022 +0800 1st init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e6b069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +go.sum \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7bad27b --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module git.sre.ink/go/gtool diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..3634033 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +gtool +======= +Golang tool set diff --git a/str/str.go b/str/str.go new file mode 100644 index 0000000..bfcab1f --- /dev/null +++ b/str/str.go @@ -0,0 +1,22 @@ +package str + +import "encoding/hex" + +type Str struct { +} + +// struing to hex +func HexEncode(str string) string { + byteStr := []byte(str) + return hex.EncodeToString(byteStr) +} + +// hex to string +func HexDecode(str string) (string, error) { + hexStr, err := hex.DecodeString(str) + if err != nil { + panic(err) + return "", err + } + return string(hexStr), nil +} diff --git a/str/str_test.go b/str/str_test.go new file mode 100644 index 0000000..fa1355b --- /dev/null +++ b/str/str_test.go @@ -0,0 +1,15 @@ +package str + +import ( + "fmt" + "testing" +) + +func TestHexEncode(t *testing.T) { + fmt.Println(HexEncode("hello world!")) + //68656c6c6f20776f726c6421 +} +func TestHexDecode(t *testing.T) { + fmt.Println(HexDecode("68656c6c6f20776f726c6421")) + //hello world! +}