go third-party common package

Configuration

go-this / these

Ini configuration file format for reading.

Address: https://github.com/Go-ini/ini

tomal

Conf format for reading the configuration file.

Address: https://github.com/BurntSushi/toml

go-yaml / yaml

Yaml format for reading the configuration file.

Address: https://github.com/go-yaml/yaml

wblog/system.go at master · wangsongyan/wblog
https://github.com/wangsongyan/wblog/blob/master/system/system.go


type Configuration struct {
    Addr    string `yaml:"addr"`
}

var config *Configuration

func LoadConfig() error {
    data, err := ioutil.ReadFile("conf/conf.yaml")
    if err != nil {
		return err
	}
	
    err = yaml.Unmarshal(data, &config)
    if err != nil {
		return err
	}
}

func GetConfig() *Configuration {
	return config
}

Journal

beego / logs

Documentation: https://beego.me/docs/module/logs.md

Example:

package util

import (
	"errors"
	"fmt"
	"github.com/astaxie/beego/logs"
)

var Logger *logs.BeeLogger

func InitLog() error {
	Logger = logs.NewLogger(10)      //缓冲区的大小
	Logger.SetLevel(logs.LevelDebug) // 设置日志写入缓冲区的等级:Debug级别(最低级别,所以所有log都会输入到缓冲区)
	Logger.EnableFuncCallDepth(true) //显示行号
	
	jsonConfig :=  fmt.Sprintf(`{"filename":"%s/log/sample.log", "daily":true,"maxdays":7,"rotate":true}`, GetCurrPath())
	err := Logger.SetLogger(logs.AdapterMultiFile, jsonConfig)
	if err != nil {
		return errors.New("init log error:" + err.Error())
	}
	Logger.Async(10) //设置缓冲 chan 的大小
	return nil
}

func GetCurrPath() string {
	file, _ := exec.LookPath(os.Args[0])
	path, _ := filepath.Abs(file)
	index := strings.LastIndex(path, string(os.PathSeparator))
	ret := path[:index]
	return ret
}

logrus

seelog

logger, err := log.LoggerFromConfigAsFile("conf/seelog.xml")
	
if err != nil {
	return err
}
	
log.ReplaceLogger(logger)

//start
seelog.Debug("something...")

Profiles?

See: https://astaxie.gitbooks.io/build-web-application-with-golang/zh/12.1.html

Session

Go standard package currently does not provide any support for the session, the need to achieve on their own. Implementation See: https://astaxie.gitbooks.io/build-web-application-with-golang/content/zh/06.2.html

You can also use beego framework has been achieved:

go get github.com/astaxie/beego/session

See: https://beego.me/docs/module/session.md

date

Email

Example:

package email

import (
	"gopkg.in/gomail.v2"
	"strconv"
)

//发送邮件
func SendMail(mailTo []string, subject string, body string) error {

	//定义邮箱服务器连接信息,如果是阿里邮箱 pass填密码,qq邮箱填授权码
	mailConn := map[string]string{
		"user": "",
		"pass": "",
		"host": "",
		"port": "",
	}

	port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int

	m := gomail.NewMessage()
	m.SetHeader("From", mailConn["user"]) //这种方式可以添加别名
	m.SetHeader("To", mailTo...)          //发送给多个用户
	m.SetHeader("Subject", subject)       //设置邮件主题
	m.SetBody("text/html", body)          //设置邮件正文

	d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])

	err := d.DialAndSend(m)
	return err

}

Timer

https://github.com/robfig/cron

reference

1、Build web application with Golang
https://astaxie.gitbooks.io/build-web-application-with-golang/zh/
2、avelino/awesome-go: A curated list of awesome Go frameworks, libraries and software
https://github.com/avelino/awesome-go
3、jobbole/awesome-go-cn: Go 资源大全中文版
https://github.com/jobbole/awesome-go-cn
4、hackstoic/golang-open-source-projects: 为互联网IT人打造的中文版awesome-go
https://github.com/hackstoic/golang-open-source-projects
5、Search · awesome-go
https://github.com/search?q=awesome-go

Guess you like

Origin www.cnblogs.com/52fhy/p/12584574.html