Table of Contents
1. Install protoc
2. Install gRPC
3. Compile rpc
Summary
Question:
Home Backend Development Golang Detailed explanation of Windows10+golang+gRPC environment construction

Detailed explanation of Windows10+golang+gRPC environment construction

Mar 15, 2021 pm 05:08 PM
golang rpc

The following tutorial column of golang will introduce you to the Windows10 golang gRPC environment construction. I hope it will be helpful to friends in need!

Detailed explanation of Windows10+golang+gRPC environment construction

1. Install protoc

Download address: https: //github.com/protocolbuffers/protobuf/release
(Note: https://github.com/protocolbuffers/protobuf is its source code library, you can learn from it. If the source code library download is too slow, you can search it on Code Cloud. Many synchronized libraries are from domestic sources, and the download speed is relatively fast. Of course, you can also create a synchronized library yourself on the code cloud)

The latest version is 3.12.2
Mine is windows10 64-bit operating system, so choose the version: protoc-3.12.2-win64.zip
You can download it directly with the browser
If the Internet speed is not enough, you can also use Thunder to download: https://github.com/protocolbuffers/ protobuf/releases/download/v3.12.2/protoc-3.12.2-win64.zip
After decompression, copy protoc.exe to the $GOPATH/bin directory
If there are multiple GOPATHs, place them in the public In the GOPATH of the third-party library, multiple projects can use it

2. Install gRPC

gRPC source code: https://github.com/grpc/grpc-go.git
The installation method given by the official website is: go get -u google.golang.org/grpc
But the following error often occurs in China:

$ go get -u google.golang.org/grpc
package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
Copy after login

Because google.golang.org is difficult to access in China , so the download will fail.
The official website also provides multiple solutions:
https://github.com/grpc/grpc-go
We use the second method to directly clone the source code to the local
Enter $ In the GOPATH/src directory, execute the command:

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
Copy after login

The download speed is sometimes fast, sometimes slow. When it is very slow, you can cancel and re-trigger. Try a few times and it will occasionally be faster.
After the download is completed, install gRPC:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install google.golang.org/grpc/
google.golang.org\grpc\credentials\credentials.go:31:2: cannot find package "github.com/golang/protobuf/proto" in any of:
        D:\Go\src\github.com\golang\protobuf\proto (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\proto (from $GOPATH)
google.golang.org\grpc\internal\binarylog\method_logger.go:28:2: cannot find package "github.com/golang/protobuf/ptypes" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes (from $GOPATH)
google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:9:2: cannot find package "github.com/golang/protobuf/ptypes/duration" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes\duration (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\duration (from $GOPATH)
google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:10:2: cannot find package "github.com/golang/protobuf/ptypes/timestamp" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOPATH)
google.golang.org\grpc\internal\transport\controlbuf.go:28:2: cannot find package "golang.org/x/net/http2" in any of:
        D:\Go\src\golang.org\x\net\http2 (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\http2 (from $GOPATH)
google.golang.org\grpc\internal\transport\controlbuf.go:29:2: cannot find package "golang.org/x/net/http2/hpack" in any of:
        D:\Go\src\golang.org\x\net\http2\hpack (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\http2\hpack (from $GOPATH)
google.golang.org\grpc\server.go:36:2: cannot find package "golang.org/x/net/trace" in any of:
        D:\Go\src\golang.org\x\net\trace (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\trace (from $GOPATH)
google.golang.org\grpc\status\status.go:34:2: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of:
        D:\Go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOROOT)
        C:\Users\ASUS\go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOPATH)
Copy after login

You can find that there will be many errors. According to the prompts, you can find that it is due to the lack of packages. I will not analyze the error information bit by bit here, but directly give the required Dependency packages and download methods (execute the command in the $GOPATH/src directory):
1) text package

git clone https://github.com/golang/text.git ./golang.org/x/text
Copy after login

2) net package

git clone https://github.com/golang/net.git ./golang.org/x/net
Copy after login

3) genproto package

git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto
Copy after login

4) Two protobuf packages
:

git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf
Copy after login
git clone https://github.com/golang/protobuf.git ./github.com/golang/protobuf
Copy after login

must be downloaded. Some of the codes of github.com/golang/protobuf depend on google.golang.org/protobuf

After downloading all the above dependent libraries, reinstall gRPC:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install google.golang.org/grpc/

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$
Copy after login

It can be seen that there are no errors and no output. . .

Verify whether gRPC is OK
Open two bash windows and execute the following instructions respectively:

go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go
Copy after login
go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go
Copy after login

Detailed explanation of Windows10+golang+gRPC environment construction

As shown in the figure, you can see that the server has received the client The message sent by the client

3. Compile rpc

For the written proto file, it needs to be compiled to become a .go file, for example:
$GOPATHgoogle.golang.orggrpcexampleshelloworldhelloworld directory There are the following files:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ ll
total 16
-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go
-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto
-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go
Copy after login

Let’s back up the .go file first

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ mv helloworld.pb.go helloworld.pb.go.bak

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ mv helloworld_grpc.pb.go helloworld_grpc.pb.go.bak

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ ll
total 16
-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go.bak
-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto
-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go.bak
Copy after login

and then execute:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go_out=plugins=grpc:. helloworld.proto
'protoc-gen-go' ????????????????????????е????
?????????????
--go_out: protoc-gen-go: Plugin failed with status code 1.
Copy after login

Found an error and need to install protoc-gen-go
Execute the following command to install:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install github.com/golang/protobuf/protoc-gen-go/

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ cd ../bin

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/bin
$ ll
total 11852
-rwxr-xr-x 1 ASUS 197121 3702272 5月  27 07:06 protoc.exe*
-rwxr-xr-x 1 ASUS 197121 8431104 6月   1 23:13 protoc-gen-go.exe*
Copy after login

After the installation is completed, the protoc-gen-go.exe file will be generated in the $GOPATH/bin directory
Then execute the compiled proto file:
A helloworld will be generated .pb.go file

Summary

There are several libraries that need to be understood:

1, https://github.com/protocolbuffers/protobuf
This is Google's open source protobuf source code library. This library contains the source code for implementing protobuf in various commonly used languages.

2, https://github.com/golang/protobuf
This library is from golang protobuf open source library, check the README.md of this library and you can find that this library has been replaced by google.golang.org/protobuf. Click this link and you can find that the source code git repository corresponding to this library is:
https://github.com/protocolbuffers/protobuf-go

Question:

Currently looking at the open source community, github’s protobuf will gradually be replaced by the google.golang.org library, but The current plug-in still has to use github's protoc-gen-go,
If you use google.golang.org/protobuf/cmd/protoc-gen-go (that is, go install google.golang.org/protobuf/cmd/protoc- gen-go, this will also generate protoc-gen-go.exe in the $GOPATH/bin directory), which will cause the following error

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go_out=plugins=grpc:. helloworld.proto
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go-grpc_out=. helloworld.proto
'protoc-gen-go-grpc' ????????????????????????е????
?????????????
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
Copy after login

There will be a separate protoc-gen-go-grpc later to generate grpc interface, but this is still in the code review stage and is to be released. . .

The above is the detailed content of Detailed explanation of Windows10+golang+gRPC environment construction. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Golang framework development practical tutorial: FAQs Golang framework development practical tutorial: FAQs Jun 06, 2024 am 11:02 AM

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

Golang's Purpose: Building Efficient and Scalable Systems Golang's Purpose: Building Efficient and Scalable Systems Apr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

See all articles