go语言中 json的omitempty标签导致protocbuf忽略默认值属性的问题

解决方法:
在生成protocol文件后,执行如下命令,去除omitempty标签即可。

Get-ChildItem -Path "C:\path\to\directory" -Recurse -Filter "*.pb.go" | ForEach-Object {
    $file = $_.FullName
    $content = Get-Content -Path $file -Encoding UTF8
    $newContent = $content -replace ',omitempty', ''
    Set-Content -Path $file -Value $newContent -Encoding UTF8
}

注:这是询问ChatGPT得出的结果,在项目中验证使用效果很理想。
ChatGPT很牛逼。

posted @ 2023-02-27 09:43  JohnRey  阅读(365)  评论(0编辑  收藏  举报