簡體   English   中英

安裝 gccgo 以測試 Protocol Buffers 3 和 Go

[英]Install gccgo for testing Protocol Buffers 3 along with Go

我正在嘗試安裝gccgo以使用 Golang 測試 Protocol Buffers 3……我必須承認,我在 8 年后(而且我不是母語人士)回到了開發領域,所以,感謝您的放縱。 謝謝 :)

所以,讀了幾遍之后,我決定從這個 repo 的 README 開始: https : //github.com/golang/protobuf

第一個要點:檢查!

協議緩沖區的最后一個版本安裝在我的 Mac 上(我的理解是protobuf-cpp-3.11.4.tar.gzhttps://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 上的說明進行操作。組織/文檔/安裝/gccgo

我的理解是我需要安裝gccgo ,它是 gcc 編譯器的一個分支。 然后我讀到gccgo實際上只是 gcc 編譯器的自定義構建,配置了--enable-languages=c,c++,go選項( 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.

因此,我從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 中編譯 protobufs,你需要有go compiler和以下包

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;
}

人.proto

下一步是將其編譯為 go 源代碼,使用protoc

protoc --go_out=. *.proto

它將生成一個 go 源代碼文件,在文件person.pb.go代表您的原始message

讓我們看看如何在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 中查看自動生成的源代碼。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM