0%

go 切片性能最佳实践
如果在for-loop里对某个slice 使用 append()请先把 slice的容量很扩充到位,这样可以避免内存重新分享以及系统自动按2的N次方幂进行扩展但又用不到,从而浪费内存

1
2
3
4
5
var keys []time.Time
for k := range dateCount {
keys = append(keys, k)
}

改成

1
2
3
4
keys := make([]time.Time, 0, len(dateCount))
for k := range dateCount {
keys = append(keys, k)
}

go 拉取代码报错

1
2
3
4
5
6
7
8
9
10
11
12
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0660 for '/root/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/root/.ssh/id_rsa": bad permissions
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. (exit status 1)
1
chmod 600 /root/.ssh/id_rsa

nginx alias 和 root 的差异
aliasroot 在 nginx 配置中的作用类似,但并不完全相同。

  1. alias: 使用 alias 指令可以将请求路径映射到指定的文件或目录,例如:

    1
    2
    3
    location /images/ {
    alias /data/images/;
    }

    在上面的配置中,请求路径为 /images/ 的请求会映射到 /data/images/ 目录下。

  2. root: 使用 root 指令可以指定当前请求的根目录,例如:

    1
    2
    3
    server {
    root /var/www/html;
    }

    在上面的配置中,如果请求的路径为 /index.html,则 nginx 会在 /var/www/html 目录下寻找该文件。

区别在于:

  • alias 指令可以指定一个与 URI 不同的目录来处理请求,即将请求路径映射到指定目录下的文件或子目录。
  • root 指令用于指定请求的根目录,nginx 在处理请求时会基于该根目录进行文件查找。

举例来说,假设有以下配置:

1
2
3
4
5
6
7
8
9
location /images/ {
alias /data/images/;
# other configurations for images location
}

location /static/ {
root /var/www/;
# other configurations for static files location
}
  • 对于 /images/ 路径的请求,使用了 alias,nginx 会将请求路径 /images/abc.jpg 映射到 /data/images/abc.jpg 文件。
  • 对于 /static/ 路径的请求,使用了 root,nginx 会将请求路径 /static/def.jpg 映射到 /var/www/static/def.jpg 文件。

因此,虽然 aliasroot 在某种程度上都可以用于指定文件或目录路径,但它们的用法和影响范围略有不同。

安装protoc-gen-go-grpc

1
2
3
4
5
6
protoc --go_out=. --go-grpc_out=. proto/ppm/*
protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
❯ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

搭文件服务器

1
2
3
4
5
6
7
8
9
10
11
12
# docker-compose.yaml
version: '2'
services:
nginx:
image: nginx:1.13
ports:
- "8080:80"
volumes:
- "./nginx.conf:/etc/nginx/nginx.conf"
- "/root/debug_file/nginx_file_home:/usr/share/nginx/files"
command:
sh -c "ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone && nginx -g 'daemon off;'"

同目录下新增 nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name your.domain.com;
root /usr/share/nginx/files;
charset utf-8;
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
}

启动容器

1
docker-compose up -d --force-recreate

为什么GOPROXY 影响 go mod tidy

1
2
3
GOPROXY="https://goproxy.cn,direct" go mod tidy 才可以
之前的 GOPROXY="https://proxy.golang.org,direct" 不行

也学到了可以把 GO 的环境变量放在同一句命令上让它生效

1
CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go build

Can json.loads ignore trailing commas
json 对象数组最后属性值后面带有一个逗号,导致 json 校验失败
我的做法是搜了最近流行的 json 库去做试验,哪个能用就用哪个,nujson 安装一直有问题
而同事的做法是去搜这个问题怎么解决,更有针对性,找到了 json5 和 jsoncomment,
最后评估用了 jsoncomment,jsont5 性能差了 200 倍。
从这件小事看出,我思考的还是太少,不要急!先多想想,最初想解决的问题是什么。

go报错 Cannot use ‘ctx’ (type *”github.com/xhs/we-api/vendor/github.com/valyala/fasthttp”.RequestCtx) as the type *”github.com/valyala/fasthttp”.RequestCtx

1
2
// Cannot use 'ctx' (type *"github.com/xhs/we-api/vendor/github.com/valyala/fasthttp".RequestCtx) as the type *"github.com/valyala/fasthttp".RequestCtx

执行 govendor sync -v 解决

aiochclient SQL语句注意事项,末尾不要加分号
因为这个包会在后面拼接上 FORMAT TSVWithNamesAndTypes,然后导致 clickhouse 以为要执行多条语句

1
2
aiochclient.exceptions.ChClientError: Code: 62. DB::Exception: Syntax error (Multi-statements are not allowed): failed at position 229 (end of query): ; FORMAT TSVWithNamesAndTypes. . (SYNTAX_ERROR) (version 23.6.2.18 (official build))