Json.Marshal对特殊字符转义

背景: 使用golang做网关进行转发请求时候,发现转发的java响应中的json中的url参数&被进行转义

  • 转发前原始数据: {"url":"http://abcd.com?param=1&a=2"}
  • 转发后: {"url":"http://abcd.com?param=1\u0026a=2"}

查看源码

1
2
3
4
5
6
7
8
// String values encode as JSON strings coerced to valid UTF-8,  
// replacing invalid bytes with the Unicode replacement rune.
// So that the JSON will be safe to embed inside HTML <script> tags,
// the string is encoded using HTMLEscape,
// which replaces "<", ">", "&", U+2028, and U+2029 are escaped
// to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029".
// This replacement can be disabled when using an Encoder,
// by calling SetEscapeHTML(false).

含义说:

1
2
3
4
5
字符串值编码为强制转换为有效UTF-8的JSON字符串,用Unicode替换符文替换无效字节。
这样就可以安全地将JSON嵌入HTML <script>标记中,并使用HTMLEscape对字符串进行编码,
HTMLEscape会将“ <”,““>”,“&”,U + 2028和U + 2029
替换为“ \” u003c”,“ \ u003e”,“ \ u0026”,“ \ u2028”和“ \ u2029”。
使用编码器时可以禁用此替换,通过调用SetEscapeHTML(false)。

测试

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import (
"bytes"
"encoding/json"
"fmt"
)

func main() {
text := "http://abcd.com?param=1&a=2"
marshal, _ := json.Marshal(text)
fmt.Println(string(marshal))

bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
jsonEncoder.Encode(text)
fmt.Println(bf.String())
}
阅读全文 »

设置

  1. 生成ssh密钥: ssh-keygen -t rsa
  2. 拷贝公钥私钥: ssh-copy-id -i ./生成的.pub username@host
  3. 配置用户.ssh目录下config
1
2
3
4
Host 192.168.0.18
HostName 192.168.0.18
User baiyz
IdentityFile ~/.ssh/llm_server
阅读全文 »

背景

今天在做开发的时候,goland引用一个go文件(长度超过2.5mb(2560000)的引用索引文件限制),此时被引用对象爆红.

原因: 因为Goland无法对过大文件进行索引只能洞察

修复

  1. 点击: Goland菜单 -> 帮助 -> 编辑自定义属性 -> 增加下面配置:
1
idea.max.intellisense.filesize=999999
  1. 重启Goland

注意设置过大可能会导致Goland卡顿!

阅读全文 »

image.png

docker文档:Turn on GPU access with Docker Compose | Docker Docs
Nvidia-Official: Installing the NVIDIA Container Toolkit — container-toolkit 1.14.1 documentation
Nvidia-Docker-Images: nvidia/cuda Tags | Docker Hub

安装Nvidia显卡Docker驱动

  1. 检查Nvidia驱动是否安装:nvidia-smi

image.png

  1. 加入apt源:

    1
    2
    3
    4
    5
    6
    curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
    && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
    && \
    sudo apt-get update
  2. 安装依赖:

    1
    sudo apt-get update && apt-get install -y nvidia-container-toolkit
  3. 更新重启docker: sudo systemctl restart docker

  4. 运行Nvidia-Container测试是否安装成功: docker run --rm --gpus all nvidia/cuda:12.2.0-runtime-ubuntu20.04 nvidia-smi

image.png

阅读全文 »