我正在尝试安装gccgo,以便用Golang测试协议缓冲器3.我必须承认,我在休假8年后回到了开发中心(而且我不是母语),所以,谢谢您的纵容。谢谢您:)
因此,经过几次阅读,我决定从回购的自述开始: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实际上是gcc编译器的自定义构建,配置了--enable-languages=c,c++,go选项( src https://golang.org/doc/install/gccgo ).那么,为什么在回购上有一个特殊的分支,它在哪里?(https://gcc.gnu.org/git.html) i
我只是放弃尝试从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
<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.因此,我从https://gmplib.org/下载了https://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 )然后,安装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示例。这将在当前目录中创建以下可执行文件: add_person_go list_people_go
make与gcc一起引入了一个单独的“规则”文件,该文件描述了如何从源代码转到已完成的程序,解释了该文件,确定了需要编译的内容,并调用了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发布于 2020-04-07 16:15:55
为了在go中编译protobufs,您需要有go compiler和下面的包
go get github.com/golang/protobuf
go get github.com/golang/protobuf/proto如果您的GOPATH包含在您的路径env中,您应该能够从您的终端执行protoc二进制文件。
让我们尝试一个简单的例子。首先定义一个protobuf模式,它表示某个对象。它看起来就像
syntax="proto3";
package main;
message Person {
string name = 1;
int32 age = 2;
}person.proto
下一步是使用protoc将其编译成go源代码。
protoc --go_out=. *.proto它将生成一个go源代码文件,表示您的proto message inm,一个文件person.pb.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中自动生成的源代码。希望这能有所帮助。
https://stackoverflow.com/questions/60858245
复制相似问题