登录
首页 >  Golang >  Go问答

在Go中安装gccgo测试Protocol Buffers 3

来源:stackoverflow

时间:2024-02-22 11:15:26 389浏览 收藏

从现在开始,努力学习吧!本文《在Go中安装gccgo测试Protocol Buffers 3》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试安装 gccgo 来使用 golang 测试 protocol buffers 3...我必须承认,我在 8 年后又回到了开发团队(而且我不是母语人士),所以,谢谢您你的放纵。谢谢:)

所以,读了几遍之后,我决定从这个仓库的 readme 开始:https://github.com/golang/protobuf

第一个要点:已选中!

我的 mac 上安装了协议缓冲区的最新版本操作系统 ( protobuf-cpp-3.11.4.tar.gz 据我了解)https://github.com/protocolbuffers/protobuf/releases/tag/v3.11.4

$ ls $gobin
dlv*           gocode*        godef*         gopkgs*        protoc-gen-go*
go-outline*    gocode-gomod*  golint*        goreturns*

第二个要点:我在这里花了几个小时......但没有成功:/

当然,从 https://golang.org/ 安装 go 编译器和工具参见 https://golang.org/doc/install 了解详细信息,或者,如果您使用 gccgo,请按照 https://golang.org/doc/install/gccgo 中的说明进行操作

我的理解是我需要安装gccgo,它是gcc编译器的一个分支。然后我读到 gccgo 实际上只是使用 --enable-languages=c,c++,go 选项配置的 gcc 编译器的自定义构建( src https://golang.org/doc/install/gccgo ) ...那么为什么仓库上有一个特殊的分支以及它在哪里? (https://gcc.gnu.org/git.html)我

我只是放弃尝试从 git 存储库下载 gccgo 分支并找到 svn 存储库:

$ svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo`
gccgo$ ./configure --enable-languages=c,c++,go
...
configure: error: building gcc requires gmp 4.2+, mpfr 3.1.0+ and mpc 0.8.0+.
try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  source code for these libraries can be found at
their respective hosting sites as well as at
.  see also
 for additional info.  if
you obtained gmp, mpfr and/or mpc from a vendor distribution package,
make sure that you have installed both the libraries and the header
files.  they may be located in separate packages.

因此,我从 https://gmplib.org/ 下载了 gmp-6.2.0.tar.lz,这导致我在解压存档之前安装 lzip

$ brew install lzip
$ lunzip gmp-6.2.0.tar.lz
$ tar - xvzf gmp-6.2.0.tar
$ cd gmp-6.2.0
gmp-6.2.0$ ./configure
gmp-6.2.0$ make
gmp-6.2.0$ make install
gmp-6.2.0$ make check ( a few warnings but every test have been passed successfully )

然后,安装mpfr-3.1.6.tar.gz

$ tar -xvzf mpfr-3.1.6.tar.gz
$ cd mpfr-3.1.6
mpfr-3.1.6$ ./configure
mpfr-3.1.6$ ./make
mpfr-3.1.6$ ./make install

...然后重试

gccgo$ ./configure --enable-languages=c,c++,go
...
the following requested languages could not be built: go
supported languages are: c,brig,c,c++,d,fortran,lto,objc,obj-c++

最后

我不确定他们在最后一步中谈论的目录...

使用“make go”在此目录中构建 go 示例。这将创建以下可执行文件 当前目录下的文件: add_person_go list_people_go

makegcc 一起引入一个单独的“规则”文件,该文件描述如何从源代码到完成的程序、解释该文件、找出需要编译的内容并调用 gcc。 (来源https://stackoverflow.com/a/768379/1216281)。所以,如果 gcc 没有正确编译,它就无法工作。

protocolbuffer$ ls
add_person.go        add_person_test.go   addressbook.proto    list_people_test.go
add_person.go.txt    addressbook.pb.go    list_people.go
protocolbuffer$ make go
make: *** no rule to make target `go'.  stop.

额外的技术。如果需要的话信息:

~$ echo $gopath
/users/me/dev/go/golib:/users/me/dev/go/code
$GOBIN is /Users/me/Dev/Go/golib/bin
$ echo $GOBIN
/Users/me/Dev/Go/golib/bin

解决方案


为了在go中编译protobuf,你需要有go编译器和以下包

go get github.com/golang/protobuf
go get github.com/golang/protobuf/proto

如果您的 gopath 包含在您的 path 环境中,您应该能够从终端执行 protoc 二进制文件。

让我们尝试一个简单的例子。首先定义一个 protobuf 模式,它代表某个对象。它可能看起来像

syntax="proto3";

package main;

message person {
      string name = 1;
      int32 age = 2;
}

下一步是使用 protoc 将其编译为 go 源代码

protoc --go_out=. *.proto

它将在文件 person.pb.go 中生成一个代表您的原型 message 的 go 源代码文件。

让我们看看如何在 main.go 中使用它

package main

import (
    "fmt"
    "os"

    "github.com/golang/protobuf/proto"
)

func main() {

    p := &person{
        name: "john doe",
        age:  30,
    }

    data, err := proto.marshal(p)
    if err != nil {
        fmt.printf("marshaling error: %v", err)
        os.exit(1)
    }

  fmt.printf("our raw protobuf object looks like: %+v\nits type is %t\n", data, data)

  // let's unmarshal it (from byte array) to an object we can use as person
    newp := &person{}
    err = proto.unmarshal(data, newp)
    if err != nil {
        fmt.printf("unmarshaling error: %v", err)
        os.exit(1)
  }

  // now we can use our unmarshaled data as a struct
  fmt.printf("newp name: %v\nnewp age: %v\nnewp type: %t\n", newp.getname(), newp.getage(), newp)

}

让我们运行一下

→ go run .
our raw protobuf object looks like: [10 8 74 111 104 110 32 68 111 101 16 30]
its type is []uint8
newP name:  John Doe
newP age:  30
newP type: *main.Person

您可以在 person.pb.go 中查看自动生成的源代码。 希望这会有所帮助。

今天关于《在Go中安装gccgo测试Protocol Buffers 3》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>