Golang Channel发送和接收
Go中Channel发送和接收操作指南 1. 前言 先来看一道面试题: 对已经关闭的 chan 进行读写,会怎么样?为什么? 在golang中channel属于较为核心的一个功能,尤其在go协程中,channel功能尤为重要;如果多个任务之间需要通信,可能就要用到通道(channel)。 2. Channel的定义 声明并初始化… 阅读更多 »Golang Channel发送和接收
Go中Channel发送和接收操作指南 1. 前言 先来看一道面试题: 对已经关闭的 chan 进行读写,会怎么样?为什么? 在golang中channel属于较为核心的一个功能,尤其在go协程中,channel功能尤为重要;如果多个任务之间需要通信,可能就要用到通道(channel)。 2. Channel的定义 声明并初始化… 阅读更多 »Golang Channel发送和接收
在Go 1.11之后推出了依赖包管理工具Go Modules之后,Go项目可以在 GOPATH 之外的位置创建,当项目中仅使用了公有库作为依赖时,使用 go get 或 go mod 更新依赖一切如初,没有任何问题。 由于Go Modules默认使用代理去更新依赖,所以当使用了私有仓库作为依赖时,Go更新依赖的相关命令将不再可用… 阅读更多 »Go Mod引用私有库
1. 获取google代码protobuf
1 2 3 4 5 6 7 8 |
# 配置go 代理 go env -w GOPROXY="https://goproxy.cn,direct" # 下载protobuf代码到${GOPATH}/pkg/google.golang.org/protobuf go get google.golang.org/protobuf # 下载protobuf指定版本 go get google.golang.org/protobuf@v1.27.1 git clone https://ghproxy.com/https://google.golang.org/protobuf |
2. 根据名称获取pb并创建结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//"github.com/golang/protobuf/jsonpb" //老版本 //"github.com/golang/protobuf/proto" //老版本 "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoregistry" // ProtoWithName is used to marshall proto message data alongside the proto // message name. type ProtoWithName struct { ProtoMsgName string ProtoMsgData string } // proto.MarshalTextString(item) 老版本 "github.com/golang/protobuf/proto" // prototext.Marshal(item) 新版本 "google.golang.org/protobuf/encoding/prototext" // MarshalJSON marshalls proto message using the marshaller from jsonpb. // The jsonpb package produces a different output than the standard "encoding/json" // package, which does not operate correctly on protocol buffers. func (p *RecordedProtoMessage) MarshalJSON() ([]byte, error) { var ( msgName string msgData string err error ) if p != nil { msgName = string(proto.MessageName(p.Message)) msgbin, err := proto.Marshal(p.Message) if err != nil { return nil, err } msgData = string(msgbin) } pwn, err := json.Marshal(ProtoWithName{ ProtoMsgName: msgName, ProtoMsgData: msgData, }) if err != nil { return nil, err } return pwn, nil } // UnmarshalJSON un-marshalls proto message using the marshaller from jsonpb. // The jsonpb package produces a different output than the standard "encoding/json" // package, which does not operate correctly on protocol buffers. func (p *RecordedProtoMessage) UnmarshalJSON(data []byte) error { var pwn ProtoWithName if err := json.Unmarshal(data, &pwn); err != nil { return err } p.ProtoMsgName = pwn.ProtoMsgName if p.ProtoMsgName == "" { return nil } //msgType := proto.MessageType(pwn.ProtoMsgName) //老版本 // 获取full name对应的message ,如果不存在则返回error msgType, err1 := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(pwn.ProtoMsgName)) if err1 != nil { return err1 } if msgType == nil { return fmt.Errorf("unknown proto message: %s", p.ProtoMsgName) } //msg := reflect.New(msgType.Elem()).Interface().(proto.Message) //老版本 msg := msgType.New().Interface() var err error if len(pwn.ProtoMsgData) > 0 && pwn.ProtoMsgData[0] == '{' { //err = jsonpb.UnmarshalString(pwn.ProtoMsgData, msg) //老版本 err = proto.Unmarshal([]byte(pwn.ProtoMsgData), msg) //protojson } else { //err = proto.UnmarshalText(pwn.ProtoMsgData, msg) //老版本 err = prototext.Unmarshal([]byte(pwn.ProtoMsgData), msg) } if err != nil { return err } p.Message = msg return nil } |
3. AnyPb 转换 proto.Message 3.1. Marshal Any [crayon-628ff24f… 阅读更多 »Golang中protobuf版本升级
当我们编写的go代码依赖特定平台或者cpu架构的时候,我们需要给出不同的实现 C语言有预处理器,可以通过宏或者#define包含特定平台指定的代码进行编译 但是Go没有预处理器,他是通过 go/build包 里定义的tags和命名约定来让Go的包可以管理不同平台的代码 这篇文章将讲述Go的条件编译系统是如何实现的,并且通过实例来… 阅读更多 »Golang使用go build 进行条件编译
1. 什么场合会使用Go与C的互操作呢? 下面的地址给出这样的答案:http://tonybai.com/2012/09/26/interoperability-between-go-and-c/ 提升局部代码性能时,用C替换一些Go代码。C之于Go,好比汇编之于C。 嫌Go内存GC性能不足,自己手动管理应用内存。 实现一些库的… 阅读更多 »Go语言学习之CGO(转载)
Go中命名为internal的package,只有该package的父级package才可以访问该package的内容。 例如,一个包的路径…/a/b/c/internal/d/e/f只能被…/a/b/c的代码层级包引入,不能被…/a/b/g或其他的任意目录引用; [官方参考文档] : https://golang.… 阅读更多 »Golang internal内部包
1. 模板渲染 Golang为模板操作提供了丰富的支持,嵌套模板、导入函数、表示变量、迭代数据等都很简单。若需要比CSV数据格式更复杂的电脑关系,模板可能是一个不错的解决方案。模板的另一个 应用是网站的页面渲染。 Golang内置text/template和html/template两个模板库,html/template库为HT… 阅读更多 »Golang模板解析和渲染