分类 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)

    }
}

graphql 有 Schema,Object,String,Int,Boolean 等元素组成。其中 Schema 包含 Query,Mutation,Subscription 三个 Object 类型的成员,对应 graphql 的三类操作。

var schema, _ = graphql.NewSchema(graphql.SchemaConfig{
    Query:    rootQuery, // 针对查询
    Mutation: rootMutation, // 针对写操作
    Subscription: rootSubscription, // 暂时没研究过,跟实时通讯相关
})

// 定义根 QueryObject
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
    Name:        "RootQuery", // Object 的类型名,必须符合变量规则,不能包含空格
    Fields: graphql.Fields{ // 该对象所包含的字段列表
        "memos": &graphql.Field{
            Type: MemoType, // 类型是下面的自定义类型
            Args: graphql.FieldConfigArgument{
                "content": &graphql.ArgumentConfig{
                    Type: graphql.String,
                },
            },
            // 每个字段可以提供一个解析函数,用来告知 graphql 如何获取数据
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                return nil, nil
            },
        },
    }
}

// 自定义Object
var MemoType = graphql.NewObject(graphql.ObjectConfig{
    Name:        "MemoType",
    Fields: graphql.Fields{
        "id": &graphql.Field{
            Name: "id",
            Type: graphql.Int,
        },
        "content": &graphql.Field{
            Name: "content",
            Type: graphql.String,
        },
    },
})
javascript调用

axios.post("/graphql", {
    "query": `mutation createMemo($content: String) { 
        createMemo(content: $content) {
            id
            content
        }
    }`,
    "variables": {
        content: "哈哈"
    }
}).then((resp) => {
    if (resp.status == 200 && !resp.data.errors) {
        console.log(resp.data.data)
    }
});