I'm trying to install gccgo
for testing Protocol Buffers 3 with Golang...I have to confess that I'm back to dev after 8 years off (and I'm not a native speaker) so, thank you for your indulgence. Thank you :)
So, After a few readings, I decided to start from the README of this repo: https://github.com/golang/protobuf
The last version os the protocol buffer is installed on my Mac ( protobuf-cpp-3.11.4.tar.gz
from my understanding) 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*
Of course, install the Go compiler and tools from https://golang.org/ See https://golang.org/doc/install for details or, if you are using gccgo, follow the instructions at https://golang.org/doc/install/gccgo
My understanding is that I need to install gccgo
which is a branch of the gcc compiler. Then I've read that gccgo
is just in fact a custom build of the gcc compiler configured with the --enable-languages=c,c++,go
option ( src https://golang.org/doc/install/gccgo ) ... so why is there a special branch on the repos and where it is? (https://gcc.gnu.org/git.html) I
I just give up to try to download the gccgo branch from the git repository and find a 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.
So, I downloaded gmp-6.2.0.tar.lz
from https://gmplib.org/ which leads me to install lzip
before untaring the archive
$ 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 )
Then, installed 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
... and try again
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++
I'm not sure about the directory they are talking about in the last step...
Build the Go samples in this directory with "make go". This creates the following executable files in the current directory: add_person_go list_people_go
make
works with gcc
to introduces a separate file of "rules", that describes how to go from source code to finished program, interprets this file, figures out what needs to be compiled, and calls gcc
. ( source https://stackoverflow.com/a/768379/1216281 ). So, if gcc it not compiled properly, it can't work.
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.
Extra tech. infos if neeeded :
~$ 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
In order to compile protobufs in go, you need to have go compiler
and the following packages
go get github.com/golang/protobuf
go get github.com/golang/protobuf/proto
If your GOPATH is included in your PATH env, you should be able to execute protoc
binary from your terminal.
Let's try a simple example. You define a protobuf
schema first, which represents some object. It can look something like
syntax="proto3";
package main;
message Person {
string name = 1;
int32 age = 2;
}
person.proto
Next step is to compile it into go source code, using the protoc
protoc --go_out=. *.proto
It will generate a go source code file represnting your proto message
inm a file person.pb.go
.
Let's see how we can use it in our 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)
}
Let's run it
→ 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
You can look at the auto-generated source code in person.pb.go. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With