GoLand之学习之路--持续更新
基础包
time
获取当前时间
t := time.Now() //2019-07-31 13:55:21.3410012 +0800 CST m=+0.006015601
//当前时间戳
t1 := time.Now().Unix() //1564552562
//时间戳转化为具体时间
fmt.Println(time.Unix(t1, 0).String())
//基本格式化的时间表示
fmt.Println(time.Now().String()) //2019-07-31 13:56:35.7766729 +0800 CST m=+0.005042501
fmt.Println(time.Now().Format("2006-01-02")) //2019-07-31
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) //2019-07-31 13:57:52
//获取第几周
_, week := time.Now().ISOWeek()
//获取年、月、日
year, month, day := rwTools.DateYmdInts()
// 获得当天零点时间
timeStr := time.Now().Format("2006-01-02")
fmt.Println("timeStr:", timeStr)
t, _ := time.Parse("2006-01-02", timeStr)
timeNumber := t.Unix()
fmt.Println("timeNumber:", timeNumber)
Bytes
多个[]byte数组合并成一个[]byte
import (
"bytes"
)
//BytesCombine 多个[]byte数组合并成一个[]byte
func BytesCombine(pBytes ...[]byte) []byte {
return bytes.Join(pBytes, []byte(""))
}
高级用法
interface
使用接口定义公共方法
package main
import "fmt"
type CheckInter interface {
Check() error
}
type CFunc func() error
func (cf CFunc) Check() error {
return cf()
}
func CodeChecker() CheckInter {
return CFunc(func() error{
return nil
})
}
func main() {
a := CodeChecker()
c,ok := a.(CFunc)
fmt.Println(a.Check())
fmt.Println(c.Check(),ok)
}
得到调用者函数名
func main() {
Foo()
}
func Foo() {
fmt.Printf("我是 %s, %s 在调用我!\n", printMyName(), printCallerName())
Bar()
}
func Bar() {
fmt.Printf("我是 %s, %s 又在调用我!\n", printMyName(), printCallerName())
}
func printMyName() string {
// func name
pc, _, _, _ := runtime.Caller(1)
return runtime.FuncForPC(pc).Name()
}
func printCallerName() string {
// func call name
pc, _, _, _ := runtime.Caller(2)
return runtime.FuncForPC(pc).Name()
}
pprof
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"time"
)
func main() {
go func() {
if err := http.ListenAndServe(":6060", nil); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
for {
err()
time.Sleep(time.Second)
fmt.Println("one")
}
}
func err() {
for i := 1; i < 100000000; i++ {
}
}
sync.Once
var once sync.Once
func GetInstance() *Tool {
once.Do(func() {
instance = new(Tool)
})
return instance
}
sync.Once 内部本质上也是双重检查的方式,但在写法上会比自己写双重检查更简洁,以下是 Once 的源码
func (o *Once) Do(f func()) {
//判断是否执行过该方法,如果执行过则不执行
if atomic.LoadUint32(&o.done) == 1 {
return
}
// Slow-path.
o.m.Lock()
defer o.m.Unlock()
//进行加锁,再做一次判断,如果没有执行,则进行标志已经扫行并调用该方法
if o.done == 0 {
defer atomic.StoreUint32(&o.done, 1)
f()
}
}
命令行参数实例
“github.com/urfave/cli”
func main() {
app := cli.NewApp()
app.Name = "hello world"
app.Usage = "say hello"
app.Flags = []cli.Flag{
cli.StringFlag{
Name:"name",
Value:"xiaomin",
},
cli.StringFlag{
Name: "word",
Usage: "what word want you say",
Value: "hello",
},
}
app.Action = func(c *cli.Context) {
name := c.String("name")
word := c.String("word")
fmt.Printf("%s say %s", name, word)
}
app.Run(os.Args)
}
使用小技巧
string和int类型相互转换
//string到int (这个默认是int32类型的)
int,err := strconv.Atoi(string)
#string到int64
int64, err := strconv.ParseInt(string, 10, 64)
//第二个参数为基数(2~36),
//第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,
//分别对应 int, int8, int16, int32和int64
#int到string
string := strconv.Itoa(int)
//等价于
string := strconv.FormatInt(int64(int),10)
#int64到string
string := strconv.FormatInt(int64,10)
//第二个参数为基数,可选2~36
//对于无符号整形,可以使用FormatUint(i uint64, base int)