我正在使用 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 文件:
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) 我无法找到内置函数的实现。
在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的新手.在下面的示例中,多个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) 我的用例是通过网络传输一组成员(整数),因此我们采用增量编码,在接收端我们解码并将整个列表作为地图,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)