我很难弄清楚protoc命令和 go 插件。
两者有什么不同:
protoc \
# Directory where you want the compiler to write your Go output.
--go_out=.
# vs ?
--go_opt=paths=source_relative
# vs ?
--go-grpc_out=.
# vs ?
--go-grpc_opt=paths=source_relative
Run Code Online (Sandbox Code Playgroud)
如果--go_opt生成
<name>.pb.go文件并--go-grpc_opt生成
<name>_grpc.pb.go文件为什么甚至有--go_out?
你能解释一下 protoc -文档没有说什么吗--go-grpc_opt?
并且甚至protoc -h不将 go 列为 OUT_DIR?
注意:我使用此文档安装
我想在 go 中使用 ed25519 生成与 openssh 兼容的 ssh 密钥来替换 rsa.GenerateKey因为 github 不再支持它。
它应该相当于:
ssh-keygen -t ed25519 -C "your_email@example.com"
Run Code Online (Sandbox Code Playgroud)
但我找不到办法做到这一点。
现在,我有这个代码:
func GenerateSSHKeys() (*ED25519Keys, error) {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
publicED25519Key, err := ssh.NewPublicKey(publicKey)
if err != nil {
return nil, err
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicED25519Key)
bytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
privBlock := pem.Block{
Type: "PRIVATE KEY",
Headers: nil,
Bytes: bytes,
} …Run Code Online (Sandbox Code Playgroud) 在下面的gRPC-client代码中,第二个是if必要的吗?
status, err := cli.GetStatus(ctx, &empty.Empty{})
if err != nil {
return err
}
if status == nil {
// this should NEVER happen - right?
return fmt.Errorf("nil Status result returned")
}
Run Code Online (Sandbox Code Playgroud)
go 直觉上,为了以防万一,应该始终检查 nil 。但是,有一个运行时检查来捕获任何客户端到服务器的nil使用情况,例如
status, err := cli.GetStatus(ctx, nil) // <- runtime error
if err != nil {
// "rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil"
return err
}
Run Code Online (Sandbox Code Playgroud)
那么是否存在类似的服务器到客户端运行时保证,从而消除检查的需要status == …
我如何知道另一端已收到我通过 gRPC 流发送的消息?
是否有一种构建方法可以在 gRPC 双向流式传输中执行此操作,或者我是否需要仅使用流式传输,然后发回响应?
原型文件:
service SimpleService {
rpc SimpleRPC (stream SimpleData) returns (stream SimpleData) {}
}
message SimpleData {
string msg = 1;
}
Run Code Online (Sandbox Code Playgroud)
去代码:
client := pb.NewSimpleServiceClient(conn)
stream, err := client.SimpleRPC(context.Background())
waitc := make(chan struct{})
msg := &pb.SimpleData{"sup"}
go func() {
for {
stream.Send(msg)
}
}()
<-waitc
stream.CloseSend()
Run Code Online (Sandbox Code Playgroud) 我对这个pprof工具有点陌生,我想知道在生产中继续运行它是否可以。从我看过的文章来看,它似乎还可以且标准,但是我对这如何不影响性能感到困惑,因为它N每秒进行一次采样,为什么这不会导致性能下降。
我正在使用mkcert生成自签名证书和授权。当我在本地使用这些文件时ListenAndServeTLS,我可以成功连接 cURL。我的主机操作系统是 MacOS。
但是,当尝试在 docker 容器中运行此 Go 代码时,出现以下错误:
x509:由未知机构签署的证书
许多其他帖子表明问题ca-certificates没有安装,应该运行:apk add ca-certificates. 我已经这样做了,我仍然有问题。
mkcert -cert-file ./cert.pem -key-file ./key.pem localhost
Run Code Online (Sandbox Code Playgroud)
这意味着证书对域“localhost”有效,可通过 https://localhost 访问。
// Load cert + key.
cert, _ := tls.LoadX509KeyPair("cert.pem", "key.pem")
// Load our CA. (Mkcert also generates this btw, check the docs).
caCert, _ := ioutil.ReadFile("rootCA.pem")
// Add our CA so it's considered an acceptable rootCA.
rootCAs, _ := x509.SystemCertPool()
rootCAs.AppendCertsFromPEM(caCert)
server := http.Server{ …Run Code Online (Sandbox Code Playgroud) 我试图停止从服务器端连接到流服务器的所有客户端。实际上我正在使用GracefulStop方法来优雅地处理它。
我正在等待os.Interrupt通道上的信号来执行 gRPC 的正常停止。server.GracefulStop()但当客户端连接时它会卡住。
func (s *Service) Subscribe(_ *empty.Empty, srv clientapi.ClientApi_SubscribeServer) error {
ctx := srv.Context()
updateCh := make(chan *clientapi.Update, 100)
stopCh := make(chan bool)
defer func() {
stopCh<-true
close(updateCh)
}
go func() {
ticker := time.NewTicker(1 * time.Second)
defer func() {
ticker.Stop()
close(stopCh)
}
for {
select {
case <-stopCh:
return
case <-ticker.C:
updateCh<- &clientapi.Update{Name: "notification": Payload: "sample notification every 1 second"}
}
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err() …Run Code Online (Sandbox Code Playgroud) 我有这个游乐场:https://go.dev/play/p/uEpYEWaQaV0 但不明白问题是什么以及为什么它不起作用!我收到错误:
invalid character entity &ccb
Run Code Online (Sandbox Code Playgroud)
我的代码是:
package main
import (
"encoding/xml"
"fmt"
)
type catalog struct {
Id string `xml:"id"`
}
type price struct {
Subtotal int `xml:"subtotal"`
Currency string `xml:"currency"`
Total int `xml:"total"`
PriceStatus string `xml:"price_status"`
}
type image struct {
Url string `xml:"url"`
Id int `xml:"id"`
}
type product struct {
Id int `xml:"id"`
RetailerId int `xml:"retailer_id"`
Image image `xml:"image"`
Price int `xml:"price"`
Currency string `xml:"currency"`
Name string `xml:"name"`
Quantity int `xml:"quantity"`
}
type order struct …Run Code Online (Sandbox Code Playgroud) 尝试获取以下代码以跳过JSON数据对象流中的解析错误噪声.基本上我希望它跳过这些ERROR: ...行并继续下一个可解析的记录.
json.Decoder有一组有限的方法 - 因此不清楚如何向前移动解码器的索引(比如一次一个字节)以移过噪声.
io.Reader有方法可以跳过来说出行的结尾(或者至少尝试跳过一个字符) - 但是这样的操作不会(理解)影响json.Decoder搜索状态.
有干净的方法吗?
https://play.golang.org/p/riIDh9g1Rx
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
)
type event struct {
T time.Time
Desc string
}
var jsonStream = `
{"T":"2017-11-02T16:00:00-04:00","Desc":"window opened"}
{"T":"2017-11-02T16:30:00-04:00","Desc":"window closed"}
{"T":"2017-11-02T16:41:34-04:00","Desc":"front door opened"}
ERROR: retrieving event 1234
{"T":"2017-11-02T16:41:40-04:00","Desc":"front door closed"}
`
func main() {
jsonReader := strings.NewReader(jsonStream)
decodeStream := json.NewDecoder(jsonReader)
i := 0
for decodeStream.More() {
i++
var ev event
if err := decodeStream.Decode(&ev); err …Run Code Online (Sandbox Code Playgroud) 我的秘密文件如下所示:
apiVersion: v1
kind: Secret
metadata:
name: secret
type: Opaque
stringData:
"user.name": "user"
"user.password": "password"
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用下一个代码获取一个值:
{{- $secret := lookup "v1" "Secret" .Release.Namespace "secret" -}}
{{- if $secret -}}
{{- print $secret.data.user.password}}
Run Code Online (Sandbox Code Playgroud)
问题是“user.password”键包含一个点,我还没有找到如何修复它。谢谢你的帮助!
在最近的问题中,使用了导致数据争用的嵌套循环。go vet只抓住了一些问题。
使用简化版本:
for _, o := range ol {
o := o // <- remove this: `go vet` does *NOT* complain
for _, d := range dl {
d := d // <- remove this: `go vet` will complain
go func() {
fmt.Printf("%03d %03d\n", o, d) // potential data-race
}()
}
}
Run Code Online (Sandbox Code Playgroud)
go vet 正确检测内部竞争条件,但不能正确检测外部竞争条件。
为什么是这样?太复杂以至于无法跟踪超过2个级别的范围?