go第三方常用包
阅读原文时间:2023年07月15日阅读:3

go-ini/ini

用于读取 ini 格式配置文件。

地址:https://github.com/Go-ini/ini

tomal

用于读取 conf 格式配置文件。

地址:https://github.com/BurntSushi/toml

go-yaml/yaml

用于读取 yaml 格式配置文件。

地址: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
}

beego/logs

文档:https://beego.me/docs/module/logs.md

示例:

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...")

配置文件?

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

目前Go标准包没有为session提供任何支持,需要自行实现。实现方法详见:https://astaxie.gitbooks.io/build-web-application-with-golang/content/zh/06.2.html

也可以使用beego框架里已经实现的:

go get github.com/astaxie/beego/session

详见:https://beego.me/docs/module/session.md

示例:

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

}

https://github.com/robfig/cron

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