安装 gccgo 以测试 Protocol Buffers 3 和 Go

Max*_*ess 5 gcc go

我正在尝试gccgo使用 Golang安装以测试 Protocol Buffers 3……我必须承认,我在 8 年后(而且我不是母语人士)回到了开发领域,所以,感谢您的放纵。谢谢 :)

所以,经过几次阅读,我决定从这个 repo 的 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*
Run Code Online (Sandbox Code Playgroud)

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

当然,从https://golang.org/安装 Go 编译器和工具,参阅 https://golang.org/doc/install了解详细信息,或者,如果您使用的是 gccgo,请按照https://golang 上的说明进行操作。组织/文档/安装/gccgo

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

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

$ 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
<https://gcc.gnu.org/pub/gcc/infrastructure/>.  See also
<http://gcc.gnu.org/install/prerequisites.html> 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.
Run Code Online (Sandbox Code Playgroud)

所以,我gmp-6.2.0.tar.lzhttps://gmplib.org/下载,这导致我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 )
Run Code Online (Sandbox Code Playgroud)

然后,安装 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
Run Code Online (Sandbox Code Playgroud)

... 然后再试一次

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++
Run Code Online (Sandbox Code Playgroud)

最后

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

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

make使用gccto 引入了一个单独的“规则”文件,它描述了如何从源代码到完成的程序,解释这个文件,找出需要编译的内容,并调用gcc. (来源/sf/answers/53786561/)。所以,如果 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.
Run Code Online (Sandbox Code Playgroud)

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

~$ echo $GOPATH
/Users/me/Dev/Go/golib:/Users/me/Dev/Go/code
Run Code Online (Sandbox Code Playgroud)
$GOBIN is /Users/me/Dev/Go/golib/bin
$ echo $GOBIN
/Users/me/Dev/Go/golib/bin
Run Code Online (Sandbox Code Playgroud)

Che*_* A. 4

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

\n\n
go get github.com/golang/protobuf\ngo get github.com/golang/protobuf/proto\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n
syntax="proto3";\n\npackage main;\n\nmessage Person {\n      string name = 1;\n      int32 age = 2;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

人物原型

\n
\n\n

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

\n\n
protoc --go_out=. *.proto\n
Run Code Online (Sandbox Code Playgroud)\n\n

它将生成一个 go 源代码文件,代表您的原始message文件person.pb.go

\n\n

让我们看看如何在我们的应用程序中使用它main.go

\n\n
package main\n\nimport (\n    "fmt"\n    "os"\n\n    "github.com/golang/protobuf/proto"\n)\n\nfunc main() {\n\n    p := &Person{\n        Name: "John Doe",\n        Age:  30,\n    }\n\n    data, err := proto.Marshal(p)\n    if err != nil {\n        fmt.Printf("marshaling error: %v", err)\n        os.Exit(1)\n    }\n\n  fmt.Printf("our raw protobuf object looks like: %+v\\nits type is %T\\n", data, data)\n\n  // let\'s unmarshal it (from byte array) to an object we can use as Person\n    newP := &Person{}\n    err = proto.Unmarshal(data, newP)\n    if err != nil {\n        fmt.Printf("unmarshaling error: %v", err)\n        os.Exit(1)\n  }\n\n  // now we can use our unmarshaled data as a struct\n  fmt.Printf("newP name: %v\\nnewP age: %v\\nnewP type: %T\\n", newP.GetName(), newP.GetAge(), newP)\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

让我们运行它

\n\n
\xe2\x86\x92 go run .\nour raw protobuf object looks like: [10 8 74 111 104 110 32 68 111 101 16 30]\nits type is []uint8\nnewP name:  John Doe\nnewP age:  30\nnewP type: *main.Person\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n