1. 安装

在网上看了很多教程,都提到要安装 protoc 与 protoc-gen-go,但通过尝试之后并不能正确安装 protoc,一下记录能够顺利安装 protoc 与 protoc-gen-go 的方法。前提是已经默认安装好了 go。

  • 安装 protoc

    • 该链接下下载protoc-3.3.0-win32.zip的包
    • 将文件解压到某一文件夹
    • 将解压出来的文件夹下的 /bin/protoc.exe 二进制的路径添加到环境变量中
  • 安装 protoc-gen-go

    • 在终端直接执行go get -u github.com/golang/protobuf/protoc-gen-go,可以在你的%GOPATH/bin路径下找到一个 protoc-gen-go.exe(这一步不能完成的话,考虑开个全局的翻墙)

至此已经完成了 protoc 与 protoc-gen-go

2. 使用

在 windows 的 cmd 尝试通过命令行生成*.pb.go 文件失败,是在 Git bash 中生成成功的。

新建一个 test.proto 文件

  1. package tutorial;
  2. message Person {
  3. required string name = 1;
  4. required int32 age = 2;
  5. optional string email = 3;
  6. }

进入test.proto 文件所在文件夹,在 Git bash 中执行
protoc --plugin=protoc-gen-go=/f/gopath/bin/protoc-gen-go.exe --go_out=./ test.proto其中 --go_out 后面是输出文件的路径与输入 proto 文件的路径,但是执行之后会出现如下错误

error

根据报错,我们知道,需要在 .proto 文件的第一行增加一行版本说明,如下


  1. syntax = "proto2";
  2. package tutorial;
  3. message Person {
  4. required string name = 1;
  5. required int32 age = 2;
  6. optional string email = 3;
  7. }
  8. }

再次运行上面的命令行,便可在你指定的目录下得到test.pb.go文件,文件内容如下:

  1. // Code generated by protoc-gen-go. DO NOT EDIT.
  2. // source: test.proto
  3. /*
  4. Package tutorial is a generated protocol buffer package.
  5. It is generated from these files:
  6. test.proto
  7. It has these top-level messages:
  8. Person
  9. */
  10. package tutorial
  11. import proto "github.com/golang/protobuf/proto"
  12. import fmt "fmt"
  13. import math "math"
  14. // Reference imports to suppress errors if they are not otherwise used.
  15. var _ = proto.Marshal
  16. var _ = fmt.Errorf
  17. var _ = math.Inf
  18. // This is a compile-time assertion to ensure that this generated file
  19. // is compatible with the proto package it is being compiled against.
  20. // A compilation error at this line likely means your copy of the
  21. // proto package needs to be updated.
  22. const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
  23. type Person struct {
  24. Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"`
  25. Age *int32 `protobuf:"varint,2,req,name=age" json:"age,omitempty"`
  26. Email *string `protobuf:"bytes,3,opt,name=email" json:"email,omitempty"`
  27. XXX_unrecognized []byte `json:"-"`
  28. }
  29. func (m *Person) Reset() { *m = Person{} }
  30. func (m *Person) String() string { return proto.CompactTextString(m) }
  31. func (*Person) ProtoMessage() {}
  32. func (*Person) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
  33. func (m *Person) GetName() string {
  34. if m != nil && m.Name != nil {
  35. return *m.Name
  36. }
  37. return ""
  38. }
  39. func (m *Person) GetAge() int32 {
  40. if m != nil && m.Age != nil {
  41. return *m.Age
  42. }
  43. return 0
  44. }
  45. func (m *Person) GetEmail() string {
  46. if m != nil && m.Email != nil {
  47. return *m.Email
  48. }
  49. return ""
  50. }
  51. func init() {
  52. proto.RegisterType((*Person)(nil), "tutorial.Person")
  53. }
  54. func init() { proto.RegisterFile("test.proto", fileDescriptor0) }
  55. var fileDescriptor0 = []byte{
  56. // 106 bytes of a gzipped FileDescriptorProto
  57. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0x49, 0x2d, 0x2e,
  58. 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x28, 0x29, 0x2d, 0xc9, 0x2f, 0xca, 0x4c, 0xcc,
  59. 0x51, 0x72, 0xe1, 0x62, 0x0b, 0x48, 0x2d, 0x2a, 0xce, 0xcf, 0x13, 0x12, 0xe2, 0x62, 0xc9, 0x4b,
  60. 0xcc, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd2, 0xe0, 0x0c, 0x02, 0xb3, 0x85, 0x04, 0xb8, 0x98, 0x13,
  61. 0xd3, 0x53, 0x25, 0x98, 0x14, 0x98, 0x34, 0x58, 0x83, 0x40, 0x4c, 0x21, 0x11, 0x2e, 0xd6, 0xd4,
  62. 0xdc, 0xc4, 0xcc, 0x1c, 0x09, 0x66, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x08, 0x07, 0x10, 0x00, 0x00,
  63. 0xff, 0xff, 0x16, 0x57, 0xf0, 0x40, 0x5c, 0x00, 0x00, 0x00,
  64. }

可以看到,protoc 根据我们的 test.proto 文件,生成的test.pb.go 中,定义了相关结构体,相关结构体中相关字段的读取方法,以及其他一些方法。

3. 问题

我在 cmd 终端下执行上述命令时,总是报错说Missing input file,但不知道为什么。而且我在 git bash 中,如果不进入到 test.proto 文件的目录下的话,也是报 Missing input file的错。这两个问题还没解决。

参考链接 https://studygolang.com/articles/11182

【转】windows 下 goprotobuf 的安装与使用的更多相关文章

  1. Windows下的Memcache安装 linux下的Memcache安装

    linux下的Memcache安装: 1. 下载 memcache的linux版本,注意 memcached 用 libevent 来作事件驱动,所以要先安装有 libevent. 官方网址:http ...

  2. Windows下的Memcache安装

    Windows下的Memcache安装: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在终端(也即cmd命令界面)下输入 'c:\memca ...

  3. Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS

    Windows下虚拟机安装Mac OS X —– VMware Workstation12安装Mac OS X 10.11 本文即将介绍WIN虚拟MAC的教程.完整详细教程(包含安装中的一些问题) [ ...

  4. coreseek实战(一):windows下coreseek的安装与测试

    coreseek实战(一):windows下coreseek的安装与测试 网上关于 coreseek 在 windows 下安装与使用的教程有很多,官方也有详细的教程,这里我也只是按着官方提供的教程详 ...

  5. Windows下Memcache的安装与在php中使用

    memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...

  6. Windows下的Memcache安装与测试教程

    Windows下的Memcache安装 1.下载memcache for windows. 下载地址:http://splinedancer.com/memcached-win32/,推荐下载bina ...

  7. Mysql在windows下的免安装配置步骤和重新安装的步骤

    windows下mysql免安装配置 1. 下载mysql免安装压缩包 下载mysql-5.6.22-winx64.zip 解压到本地D:\mysql-5.6.22-winx64 2. 修改配置文件 ...

  8. DEDECMS最新5.7版在Windows下的Memcache安装

    一,织梦后台后台设置进入系统后台,在[系统基本参数]下面的"性能选项"卡当中,关于memcache进行如下配置: cfg_memcache_enable : 是否启用memcach ...

  9. Windows下的Memcache安装:

    Windows下的Memcache安装:1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在终端(也即cmd命令界面)下输入 'c:\memcac ...

随机推荐

  1. 多线程-synchronized

    引言 synchronized是Java线程同步中的一个重要的概念,synchronized是独占锁(互斥锁),同时也是可重入锁(可重入锁一定程度上避免了死锁的问题,内部是关联一个计数器,加一次锁计数 ...

  2. iOS开发25个性能调优技巧

    1. 用ARC管理内存 ARC(Automatic Reference Counting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为 ...

  3. H3C-WA2210升级

    前几天升级了2210固件,做个记录 首先吐槽一点:华为官方给出的公共下载账号yx800根本没用..下不了官方提供的固件资料 组网准备: 升级之前要先看看原机版本(dis ver): 在升级版本之前,请 ...

  4. 用mysql写带占位符的select语句

    sql.append(" AND t.f_user_name LIKE CONCAT('%',?,'%')");//模糊查询 sql.append(" AND t.f_u ...

  5. JdbcTemplate中的exectue和queryForList方法的性能对比

    @Autowired JdbcTemplate jdbcParam; pstm =                 jdbcParam.getDataSource()                ...

  6. java @override 报错处理

    转载自:http://blog.sina.com.cn/s/blog_9c7605530101kl9r.html 一.java @override 报错处理 做项目的时候,同事那边电脑上编译通过的ja ...

  7. shell 实现mysql写入操作

    mysql -uroot study -proot << EOF > insert into top_n_movie(movie,sumprice)values('hello kit ...

  8. Unix系统编程()通用模型以外的操作ioctl

    之前学习到的都是通用的IO模型,现在要学的是一个ioctl系统调用,ioctl为执行文件和设备提供了一种多用途机制. int ioctl(int fd, int request, - /*argp*/ ...

  9. php hash_hmac 与python hmac 区别

    使用 HMAC 方法生成带有密钥的哈希值 hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = fal ...

  10. PHP中钩子函数的实现与认识

    PHP中钩子函数的实现与认识 分类:PHP编程  作者:rming  时间:2014-09-21 假如有这么一段程序: function fun(){ fun1(); fun2(); }   首先程序 ...