分类 golang 下的文章

结构化日志方便查询

package main

import (
    "errors"

    "go.uber.org/zap"
)

func main() {
    // logger, err := zap.NewProduction()
    logger, err := zap.NewDevelopment()
    if err != nil {
        panic(err.Error())
    }
    logger.Info("hello world", zap.String("user", "咪了个猫"), zap.Int("age", 666))

    err = errors.New("我是一个错误")
    logger.Error("报错啦", zap.Error(err), zap.String("xxx", "我是一个参数"))

    logger.Info("hello world", zap.String("user", "咪了个猫"), zap.Error(err))
}


2020-09-22T16:51:33.156+0800    INFO    xxx3/main.go:15 hello world     {"user": "咪了个猫", "age": 666}
2020-09-22T16:51:33.156+0800    ERROR   xxx3/main.go:18 报错啦  {"error": "我是一个错误", "xxx": "我是一个参数"}
main.main
        /Users/wameidemao/Desktop/xxx3/main.go:18
runtime.main
        /usr/local/go/src/runtime/proc.go:204
2020-09-22T16:51:33.156+0800    INFO    xxx3/main.go:20 hello world     {"user": "咪了个猫", "error": "我是一个错误"}

智能家居

hc

https://github.com/brutella/hc

hc is a lightweight framework to develop HomeKit accessories in Go. It abstracts the HomeKit Accessory Protocol (HAP) and makes it easy to work with services and characteristics.

hc handles the underlying communication between HomeKit accessories and clients. You can focus on implementing the business logic for your accessory, without having to worry about the protocol.

import (
    "net/http"
    "strings"
    urlpkg "net/url"
)

url := "http://jenkins/job/test/buildWithParameters"
data := urlpkg.Values{}
data.Add("SERVER_NAME", "测试服")
data.Add("SERVER_URI", "refs/heads/master")
data.Add("CONFIG_URI", "refs/heads/master")

req, err := http.NewRequest("POST", url, strings.NewReader(data.Encode()))
if err != nil {
    panic(err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth("wameidemao", "1151edd79316129baea8911ce078908130")
resp, err := http.DefaultClient.Do(req)

package main

import "github.com/rjeczalik/notify"
import "fmt"

func main() {
    // Make the channel buffered to ensure no event is dropped. Notify will drop
    // an event if the receiver is not able to keep up the sending pace.
    c := make(chan notify.EventInfo, 1)

    // Set up a watchpoint listening on events within current working directory.
    // Dispatch each create and remove events separately to c.
    if err := notify.Watch(".", c, notify.Create, notify.Write, notify.Rename, notify.Remove); err != nil {
        fmt.Println("Error:", err)
    }
    defer notify.Stop(c)

    // Block until an event is received.
    for ie := range c {

     fmt.Println("Got event:", ie)

    }
}