Documentation
¶
Overview ¶
Package my provides some useful utility functions
Index ¶
- Variables
- func AppPath() string
- func AppendText(filepath string, text string) (err error)
- func BytesToString(b []byte) string
- func CDate(s string) time.Time
- func CInt(s string) int
- func CStr(i int) string
- func DownloadFile(url, filepath string) (err error)
- func FileExist(path string) bool
- func FolderExist(path string) bool
- func FormatDateTime(t time.Time, mode int) string
- func FriendlyTime(t time.Time) string
- func GenRandom(n, charType int) string
- func GetJSON(url string, v interface{}) error
- func GetURL(url string) (reply []byte, err error)
- func HMACSHA1(s, key string) string
- func HMACSHA256(s, key string) string
- func IsEmpty(s string) bool
- func Left(s string, n int) string
- func Len(s string) int
- func LoadJSONConfig(fn string, v interface{}) error
- func LoadXMLConfig(fn string, v interface{}) error
- func MD5(s string) string
- func MakeDir(dir string) error
- func Mid(s string, start, length int) string
- func MustLoadConfig(fn string, v interface{})
- func Now() string
- func PostJSON(url string, params string) (reply []byte, err error)
- func PostURL(url string, params string) (reply []byte, err error)
- func ReadText(filepath string) (text string, err error)
- func Right(s string, n int) string
- func RndAlpha(n int) string
- func RndFilename(ext string) string
- func RndNumber(n int) string
- func RndString(n int) string
- func SHA1(s string) string
- func SHA256(s string) string
- func Space(count int) string
- func StringToBytes(s string) []byte
- func Test(str, pattern string) bool
- func WriteText(filepath string, text string) (err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // RequestTimeout set the maximum time for a request. RequestTimeout time.Duration = 10 )
Functions ¶
func AppPath ¶ added in v0.0.2
func AppPath() string
AppPath returns an absolute path of app.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
dir := my.AppPath()
// dir is your app startup dir
dir = "c:\\fakeapp"
fmt.Println(dir)
}
Output: c:\fakeapp
func AppendText ¶
AppendText append a string to a text file.
func BytesToString ¶ added in v0.0.2
BytesToString Convert a byte slice to a string.
func CDate ¶
CDate returns the result of time.ParseInLocation.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
fmt.Println(my.CDate("1982-2-11 20:13:14").Format("2006-01-02 15:04:05"))
fmt.Println(my.CDate("1982-02-11 20:13:14").Format("2006-01-02 15:04:05"))
fmt.Println(my.CDate("1982-02-11").Format("2006-01-02 15:04:05"))
fmt.Println(my.CDate("82-02-11").Format("2006-01-02 15:04:05")) //unsupport
}
Output: 1982-02-11 20:13:14 1982-02-11 20:13:14 1982-02-11 00:00:00 1900-01-01 00:00:00
func DownloadFile ¶ added in v0.0.2
DownloadFile download a file from a url.
Example ¶
ms := mockServer()
defer ms.Close()
localfile := "testdata/demo.txt"
err := my.DownloadFile(ms.URL+"/file/demo.txt", localfile)
if err != nil {
log.Println(err)
}
os.Remove(localfile) //clean up
fmt.Println("OK")
Output: OK
func FileExist ¶ added in v0.0.2
FileExist returns true if a specified file exists; false if it does not.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
fmt.Println(my.FileExist("testdata/config.json"))
fmt.Println(my.FileExist("testdata/notexist.txt"))
}
Output: true false
func FolderExist ¶ added in v0.0.2
FolderExist returns true if a specified folder exists; false if it does not.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
fmt.Println(my.FolderExist("testdata"))
fmt.Println(my.FolderExist("testdata2"))
}
Output: true false
func FormatDateTime ¶ added in v0.0.2
FormatDateTime returns an expression formatted as a date or time.
Example ¶
package main
import (
"fmt"
"time"
"github.com/zs5460/my"
)
func main() {
birthday := time.Date(1982, time.February, 11, 20, 13, 14, 0, time.Local)
fmt.Println(my.FormatDateTime(birthday, 0))
fmt.Println(my.FormatDateTime(birthday, 1))
fmt.Println(my.FormatDateTime(birthday, 2))
fmt.Println(my.FormatDateTime(birthday, 3))
fmt.Println(my.FormatDateTime(birthday, 4))
}
Output: 1982-02-11 20:13:14 1982-02-11 02-11 20:13:14 20:13
func FriendlyTime ¶
FriendlyTime returns easy to read format.
Example ¶
package main
import (
"fmt"
"time"
"github.com/zs5460/my"
)
func main() {
fmt.Println(my.FriendlyTime(time.Now().Add(-2 * time.Second)))
fmt.Println(my.FriendlyTime(time.Now().Add(-2 * time.Minute)))
fmt.Println(my.FriendlyTime(time.Now().Add(-2 * time.Hour)))
fmt.Println(my.FriendlyTime(time.Now().Add(-2 * 24 * time.Hour)))
longlongago, _ := time.Parse("2006-01-02 15:04:05", "1982-02-11 20:13:14")
fmt.Println(my.FriendlyTime(longlongago))
}
Output: 刚刚 2分钟前 2小时前 2天前 1982-02-11
func GetJSON ¶ added in v0.0.2
GetJSON get json from a url and unmarshal to a struct.
Example ¶
ms := mockServer()
defer ms.Close()
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Data []interface{} `json:"data"`
Total int `json:"total"`
}
var ret Result
err := my.GetJSON(ms.URL+"/json", &ret)
if err != nil {
return
}
fmt.Println(ret.Code)
Output: 0
func GetURL ¶
GetURL request a url.
Example ¶
ms := mockServer()
defer ms.Close()
s, err := my.GetURL(ms.URL + "/ping")
if err != nil {
return
}
fmt.Println(string(s))
Output: PONG
func HMACSHA256 ¶ added in v0.0.2
HMACSHA256 generate a keyed hash value using the HMAC method.
func IsEmpty ¶
IsEmpty returns a Boolean value indicating whether a string is blank.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
var s string
fmt.Println(my.IsEmpty(s))
s = "zs5460"
fmt.Println(my.IsEmpty(s))
}
Output: true false
func Left ¶
Left returns a specified number of characters from the left side of a string.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
s := "zs5460"
fmt.Println(my.Left(s, 2))
s = "公元2000年"
fmt.Println(my.Left(s, 5))
}
Output: zs 公元200
func Len ¶
Len returns the number of rune in a string.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
s := "zs5460"
fmt.Println(my.Len(s))
s = "公元2000年"
fmt.Println(my.Len(s))
}
Output: 6 7
func LoadJSONConfig ¶ added in v0.0.2
LoadJSONConfig load config from json file.
Example ¶
package main
import (
"fmt"
"log"
"github.com/zs5460/my"
)
func main() {
type config struct {
AppName string
Version string
}
var c config
err := my.LoadJSONConfig("testdata/config.json", &c)
if err != nil {
log.Fatal(err)
}
fmt.Println(c.AppName)
fmt.Println(c.Version)
}
Output: DEMO 1.0.0
func LoadXMLConfig ¶ added in v0.0.2
LoadXMLConfig load config from xml file.
Example ¶
package main
import (
"fmt"
"log"
"github.com/zs5460/my"
)
func main() {
type config struct {
AppName string `xml:"appname"`
Version string `xml:"version"`
}
var c config
err := my.LoadXMLConfig("testdata/config.xml", &c)
if err != nil {
log.Fatal(err)
}
fmt.Println(c.AppName)
fmt.Println(c.Version)
}
Output: DEMO 1.0.0
func Mid ¶
Mid returns a specified number of characters from a string.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
s := "zs5460"
fmt.Println(my.Mid(s, 2, 2))
s = "公元2000年"
fmt.Println(my.Mid(s, 2, 4))
}
Output: 54 2000
func MustLoadConfig ¶ added in v0.0.2
func MustLoadConfig(fn string, v interface{})
MustLoadConfig load config or panic.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
type config struct {
AppName string `xml:"appname"`
Version string `xml:"version"`
}
var c config
my.MustLoadConfig("testdata/config.xml", &c)
fmt.Println(c.AppName)
fmt.Println(c.Version)
}
Output: DEMO 1.0.0
func PostURL ¶
PostURL request a url with POST method. params is a string like k1=v1&k2=v2
Example ¶
ms := mockServer()
defer ms.Close()
s, err := my.PostURL(ms.URL+"/post", "name=zs")
if err != nil {
return
}
fmt.Println(string(s))
Output: name:zs
func Right ¶
Right returns a specified number of characters from the right side of a string.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
s := "zs5460"
fmt.Println(my.Right(s, 2))
s = "公元2000年"
fmt.Println(my.Right(s, 5))
}
Output: 60 2000年
func RndFilename ¶
RndFilename returns a file name consisting of a time and a random string.
func Space ¶
Space returns a string consisting of the specified number of spaces.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
s := "a" + my.Space(5) + "b"
fmt.Println(s)
fmt.Println("1234567")
}
Output: a b 1234567
func StringToBytes ¶ added in v0.0.2
StringToBytes Convert a string to a byte slice.
func Test ¶
Test provides some common regular expression detection features.
Example ¶
package main
import (
"fmt"
"github.com/zs5460/my"
)
func main() {
// pattern can be english,chinese,idcard,email,qq,zip,phone,mobile,url,username,password.
s := "zs5460"
fmt.Println(my.Test(s, "english"))
s = "zhousong"
fmt.Println(my.Test(s, "english"))
s = "zs5460.gmail.com"
fmt.Println(my.Test(s, "email"))
s = "zs5460@gmail.com"
fmt.Println(my.Test(s, "email"))
}
Output: false true false true
Types ¶
This section is empty.