Use protobuf in go

Article Directory

Compile

The steps to refer to [1] [2] are as follows:

  • 1. Install the compiler.
    It is easy to miss here. At the beginning, because there was no such step, it was impossible to find protoc to
    download the corresponding system compression package. After decompression, put protoc.exe in gopath / bin, which is under the Go installation path. In the bin
  • 2. Install protobuf-go
go install google.golang.org/protobuf/cmd/protoc-gen-go

Encountered the following error

can't load package: package google.golang.org/protobuf/cmd/protoc-gen-go: cannot find package "google.golang.org/protobuf/cmd/protoc-gen-go" in any of:
        C:\Go\src\google.golang.org\protobuf\cmd\protoc-gen-go (from $GOROOT)
        C:\Users\peikai\go\src\google.golang.org\protobuf\cmd\protoc-gen-go (from $GOPATH)

If you get the same error, get it first

go get google.golang.org/protobuf/cmd/protoc-gen-go
  • Compile
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto

When developing by yourself, directly cd to the * .proto directory and execute the following simple command:

protoc --go_out=. *.proto

use

Types of

Refer to the section of Scalar Value Types in [3]

reference

[1] tutorial
[2] Use of protobuf in Go
[3] Scalar Value Types

Published 41 original articles · praised 7 · 20,000+ views

Guess you like

Origin blog.csdn.net/pkxpp/article/details/105671316