小编zaR*_*Roc的帖子

如何将 JSON 数组建模为 protobuf 定义

我正在使用 protobuf 在 golang 中编写一项新服务。我想在 .proto 文件中对以下请求 JSON 进行建模。

[
   {
    "var": ["myVariable1","myVariable2"], 
    "key1": 123123,
    "key2": 1122,
    "key3": "abcd-0101"
   },
  { 
    "var": ["myVariable1"], 
    "key1": 123124,
    "key2": 1123,
    "key3": "abcd-0102"
  },
] 
Run Code Online (Sandbox Code Playgroud)

目前存在两个问题:

  • 每个数组元素中的键事先未知,因此我无法在 .proto 文件中创建消息并使其重复。我需要保留它的地图
  • 我无法对 json 进行建模,它只是一个没有键的数组。每次我这样做时,都会显示以下错误:无法解码请求:json:无法将数组解组为Go值

以下是我的 .proto 文件:

syntax = "proto3";

package pb;

import "google/protobuf/empty.proto";
import "google/api/annotations.proto";

service Transmitter {
  rpc GetVariables(GetVariablesRequest) returns (GetVariablesResponse) {
    option (google.api.http) = {
                post: "/api/v1/{Service}/getVars"
                body: "*"
            };
  };
}


message GetVariablesRequest {
  string Service = 1;
  repeated GetVarInput in = …
Run Code Online (Sandbox Code Playgroud)

arrays json go protocol-buffers grpc

6
推荐指数
1
解决办法
2万
查看次数

Golang的内置函数实现

我无法找到内置函数的实现。

在builtin.go中,这是我发现的:

// The copy built-in function copies elements from a source slice 
into a
// destination slice. (As a special case, it also will copy bytes 
from a
// string to a slice of bytes.) The source and destination may 
overlap. Copy
// returns the number of elements copied, which will be the minimum 
of
// len(src) and len(dst).
func copy(dst, src []Type) int
Run Code Online (Sandbox Code Playgroud)

内置函数的实际实现在哪里?

go

5
推荐指数
1
解决办法
2814
查看次数

GoLand 文件缓存冲突

我不断收到文件缓存冲突的弹出窗口,但是当我进入显示差异时,内存和磁盘更改都是相同的。

极大地阻碍了工作。知道如何解决吗?

在此处输入图片说明

goland

2
推荐指数
1
解决办法
762
查看次数

从通道消耗的多个例程导致数据丢失

我是Go的新手.在下面的示例中,多个go例程正在从无缓冲的通道中消耗.

代码:

var c = make(chan int)

func f() {
    for val := range c {    
        fmt.Printf("routine 1 : %v\n", val)
    }
}   

func g() {
    fmt.Printf("routine 2 : %v\n", <-c)
}

func main() {
    go f()
    go g()
    c <- 0
    c <- 1
    c <- 2
    c <- 3
    c <- 4
    c <- 5
    close(c) 
}
Run Code Online (Sandbox Code Playgroud)

输出是:

routine 1 : 0
routine 1 : 2
routine 2 : 1
routine 1 : 3
routine 1 : 4 …
Run Code Online (Sandbox Code Playgroud)

go goroutine

0
推荐指数
1
解决办法
922
查看次数

go map的内存高效实现?

我的用例是通过网络传输一组成员(整数),因此我们采用增量编码,在接收端我们解码并将整个列表作为地图,map[string]struct{} 的复杂度为 O(1)用于会员检查。

我面临的问题是,对于 200 万个整数,成员的实际大小仅为 15MB,但堆中的地图大小为 100+MB。似乎 Go 的实际地图实现不适用于大型地图。由于它是客户端 SDK,我不想对可用内存产生太大影响,并且可能有多个这样的组需要长时间保存在内存中 - 大约 1 周。

Go 中有更好的替代 DS 吗?

type void struct{}
func ToMap(v []int64) map[string]void {
 out := map[string]void{}
 for _, i := range v {
   out[strconv.Itoa(int(i))] = void{}
 }
 return out
}
Run Code Online (Sandbox Code Playgroud)

maps heap-memory go

0
推荐指数
1
解决办法
2007
查看次数

标签 统计

go ×4

arrays ×1

goland ×1

goroutine ×1

grpc ×1

heap-memory ×1

json ×1

maps ×1

protocol-buffers ×1