package main

import "fmt"

var VERSION string
var PLATFORM string

func main() {
    fmt.Println("当前版本:"+VERSION)
}

go build -ldflags="-X main.VERSION=v1.0.1 -X main. PLATFORM=android"

xxx, err := json.Marshal(data)
if err != nil {
    return nil, err
}

resp, err := http.Post(url, "application/json", bytes.NewReader(xxx))
if err != nil {
    return nil, err
}
defer resp.Body.Close()

var data2 map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
    return nil, err
}

return data2, nil

列出网络

networksetup -listallnetworkservices

Ethernet
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

打开关闭 SOCKS

networksetup -setsocksfirewallproxystate <networkservice> <on off>

修改 SOCKS

networksetup -setsocksfirewallproxy <networkservice> <domain> <port number> <authenticated> <username> <password>

FROM scratch
WORKDIR /
COPY aaa /
ENV TZ=Asia/Shanghai
ENTRYPOINT ["/aaa"]

docker build --no-cache -t u2nn/aaa:v2 .

默认 docker push 推送至 hub.docker.com,也可以搭建私有 registey

标签命名规则:[主机地址[:端口号]/]名称/名称/名称:版本号
docker.yqmiot.com:5000/u2nn/yqmiot/yqmnet:v100

任何地方 docker pull u2nn/aaa:v2 可以直接运行。

其他一些环境变量
ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn,direct

参考资料:
https://segmentfault.com/a/1190000039727679

package main

import (
    "net/http"

    "github.com/gorilla/rpc/v2"
    "github.com/gorilla/rpc/v2/json2"
)

type WebService struct {
}

type LoginReq struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

type LoginResp struct {
    Token string `json:"token"`
}

func (*WebService) Login(r *http.Request, req *LoginReq, resp *LoginResp) error {
    if len(req.Username) < 4 ||
        len(req.Username) > 12 {
        return &json2.Error{
            Code:    1001,
            Message: "用户名长度错误",
            Data:    req.Username,
        }
    }
    resp.Token = "恭喜你登录成功!!!~~~"
    return nil
}

func main() {
    s := rpc.NewServer()
    s.RegisterCodec(json2.NewCodec(), "application/json")
    s.RegisterService(&WebService{}, "Web")
    http.Handle("/webrpc", s)
    http.ListenAndServe(":8000", nil)
}
curl http://localhost:8000/webrpc -X POST -H 'Content-Type: application/json' -d '{"id":0, "jsonrpc":"2.0", "method":"Web.Login", "params":[{"username": "daqiu"}]}'

{"jsonrpc":"2.0","result":{"token":"恭喜你登录成功!!!~~~"},"id":0}
跨域问题
r.POST("/webrpc", func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        s.ServeHTTP(c.Writer, c.Request)
    })
    r.OPTIONS("/webrpc", func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Max-Age", "86400")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "*")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
        c.Writer.Header().Set("Access-Control-Expose-Headers", "*")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(http.StatusNoContent)
        } else {
            c.Next()
        }
    })