小编use*_*681的帖子

protoc-gen-go 和 protoc-gen-go-grpc 的区别

我对protoc-gen-govs有点困惑protoc-gen-go-grpc。我知道:

  • protoc-gen-go 包含用于 protobuf 消息序列化/反序列化的代码
  • protoc-gen-go-grpc 包含 gRPC 服务器和客户端的代码

但是,我正在使用以下命令

protoc -I $protodir --go_out=plugins=grpc:./genproto/ $protodir/v1/foo.proto
Run Code Online (Sandbox Code Playgroud)

并且生成的foo.pb.go包含用于消息序列化gRPC 服务器/客户端的代码。另外,我只protoc-gen-go安装在我的系统上。

我有一种感觉,这是在 GO 中进行 gRPC旧方法,新方法protoc --go_out=. --go-grpc_out=.

问题:

  1. 为什么这仅适用于protoc --go_out=.(为什么它还生成 gRPC 客户端/服务器代码?)
  2. 使用一种方法与另一种方法相比有什么优势?

谢谢,直流

grpc-go

7
推荐指数
1
解决办法
1668
查看次数

范围通道以死锁结束

以下代码以致命错误结束:所有 goroutine 都处于睡眠状态 - 死锁!

// Package letter returns the frequency of letters in texts using parallel computation.
package letter

import "fmt"

const testVersion = 1

type FreqMap map[rune]int

func Frequency(s string) FreqMap {
    m := FreqMap{}
    for _, r := range s {
        m[r]++
    }
    return m
}

func ConcurrentFrequency(l []string) FreqMap {
    ch := make(chan FreqMap)
    for _, s := range l {
        go func() {
            ch <- Frequency(s)
        }()
    }
    m := FreqMap{}
    for c := range …
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
1
解决办法
2068
查看次数

用*实例化var时,单例测试不起作用

我遵循本书(https://github.com/PacktPublishing/Go-Design-Patterns/blob/master/Chapter02/main.go)中所述的Singleton设计模式,并且此代码位于文件“ singleton2”中。走”:

package singleton2

type Singleton interface {
  AddOne() int
}

type singleton struct {
  count int
}

//var instance = &singleton{}
var instance *singleton

func GetInstance() *singleton {
  if instance == nil {
    return new(singleton)
  }
  return instance
}

func (s *singleton) AddOne() int {
  s.count++
  return s.count
}
Run Code Online (Sandbox Code Playgroud)

然后,我有此测试文件(singleton2_test.go):

package singleton2

import "testing"

func TestGetInstance(t *testing.T) {
  counter1 := GetInstance()
  if counter1 == nil {
    t.Fatal("expected pointer to Singleton after calling GetInstance(), not nil")
  }

  expectedCounter …
Run Code Online (Sandbox Code Playgroud)

go

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

标签 统计

go ×2

grpc-go ×1