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发送和接收
1. 概述 添加行: df.loc[]以及df.append()两种方法 添加列: df[]和df.insert()两种方法 添加行列: concat()和reindex()两种方法 loc bug解决 2. 添加行 2.1. 采用loc[]方法 loc方法和iloc方法一样,可以索引DataFrame数据,一般是通过data.… 阅读更多 »Python pandas中DataFrame中增删行列
1. 引入numpy第三方库 首先我们引入numpy这个第三方库,如果有同学没安装numpy可在命令行中pip install numpy进行安装(Mac用户 sudo pip3 install numpy) 测试采用python版本3.7.0 64bit
1 |
import numpy as np |
在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-628fdac6… 阅读更多 »Golang中protobuf版本升级
运行时候出错,如下:“OSError: [WinError 193] %1 不是有效的 Win32 应用程序。”,顺着往上一看,是numpy等包的问题,果然是它的问题:: 此时出现如题所描述的错误,大概率因为昨天卸载了本来的32位python,重装了64位,而里面的库仍然是32位,64位程序读取32位dll文件时发生错误,所以产… 阅读更多 »已解决:Python OSError: [WinError 193] %1 不是有效的 Win32 应用程序。(numpy模块)
当我们编写的go代码依赖特定平台或者cpu架构的时候,我们需要给出不同的实现 C语言有预处理器,可以通过宏或者#define包含特定平台指定的代码进行编译 但是Go没有预处理器,他是通过 go/build包 里定义的tags和命名约定来让Go的包可以管理不同平台的代码 这篇文章将讲述Go的条件编译系统是如何实现的,并且通过实例来… 阅读更多 »Golang使用go build 进行条件编译