Название: Practical Go
Автор: Amit Saha
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119773832
isbn:
3 Finally, add the $HOME/.local/bin directory to your $PATH environment variable: $ export PATH="$PATH:$HOME/.local/bin" in your shell's initialization script, such as $HOME/.bashrc for Bash shell and .zshrc for Z shell.
Once you have completed the preceding steps, open a new terminal window, and run the command protoc --version
:
$ protoc --version libprotoc 3.16.0
If you see output like the one above, you are ready to move on to the next step.
To install the protobuf plug-in for Go, protoc-gen-go
(release v1.26), run the following command from a terminal window:
$ go install google.golang.org/protobuf/cmd/[email protected]
To install the gRPC plug-in for Go, protoc-gen-go-grpc
(release v1.1) tool, run the following command:
$ go install google.golang.org/grpc/cmd/[email protected]
Then add the following to your shell's initialization file ($HOME/.bashrc or $HOME/.zshrc)
:
$ export PATH="$PATH:$(go env GOPATH)/bin"
Open a new terminal window, and run the following commands:
$ protoc-gen-go --version protoc-gen-go v1.26.0 $ protoc-gen-go-grpc --version protoc-gen-go-grpc 1.1.0
If you see an output like above, the tools have been installed successfully.
Windows
NOTE You will need to open a Windows PowerShell window as an administrator to run the steps.
To install the protocol buffers compiler, run the following steps:
1 Download the latest release (3.16.0 at the time of this book's writing) file from https://github.com/protocolbuffers/protobuf/releases, corresponding to your architecture. Look for a file named protoc-3.16.0-win64.zip in the Assets section.
2 Then create a directory where you will store the compiler. For example, in C:\Program Files as follows: PS C:\> mkdir 'C:\Program Files\protoc-3.16.0' .
3 Next, extract the downloaded .zip file inside that directory. Run the following command while you are inside the directory where you downloaded the .zip file: PS C:\> Expand-Archive.\protoc-3.16.0-win64\ -DestinationPath 'C:\Program Files\protoc-3.16.0 ’.
4 Finally, update the Path environment variable to add the above path: PS C:\> [Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\protoc-3.16.0\bin", "Machine").
Open a new PowerShell window, and run the command protoc --version
:
$ protoc --version libprotoc 3.16.0
If you see an output like the one above, you are ready to move on to the next step.
To install the protobuf compiler for Go, protoc-gen-go tool
(release v1.26), run the following command from a terminal window:
C:\> go install google.golang.org/protobuf/cmd/[email protected]
To install the gRPC plug-in for Go, protoc-gen-go-grpc
(release v1.1) tool, run the following command:
C:\> go install google.golang.org/grpc/cmd/[email protected]
Open a new Windows PowerShell Window, and run the following commands:
$ protoc-gen-go --version protoc-gen-go v1.26.0 $ protoc-gen-go-grpc --version protoc-gen-go-grpc 1.1.0
If you see an output like the one above, the tools have been installed successfully.
Installing Docker Desktop
For the last chapter in the book, you will need the ability to run applications in software containers. Docker Desktop (https://www.docker.com/get-started
) is an application that allows us to do that. For macOS and Windows, download the installer from the above website corresponding to your operating system and architecture, and follow the instructions to complete the installation.
For Linux, the installation steps will vary depending on your distribution. See https://docs.docker.com/engine/install/#server
for detailed steps for your specific distribution. I also recommend that for ease of use (not recommended for production environments), you configure your docker installation to allow non-root users to run containers without using sudo
.
Once you have followed the installation steps for your specific operating system, run the following command to download a docker image from Docker Hub and run it to ensure that the installation has been successfully completed:
$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 109db8fad215: Pull complete Digest: sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d 623fcb2250e8bec85b38 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ..
That completes our software installation for the book. Next, we will quickly cover some conventions used throughout the book.
Guide to the Book
In the following sections, you will learn various bits and pieces of information that will help you get the most out of the book. First, I discuss the choice of the module path for the code listings.
Go Modules
In this book, all applications will start by initializing a module as the first step. This will translate to running the go
command, go mod init <module path>
. Throughout the book, I have used a “placeholder” module path, which is github.com/username/<application-name>
. Thus, in applications where we have written our module to consist of more than one package, the import path looks like this: github.com/username/<application-name>/<package>
.
You can use these module paths if you are not planning to share these applications. If you plan to share your applications, or develop them further, you are encouraged to use your own module path, which is pointing to your own repository, likely a Git repository hosted on https://bitbucket.org
, https://github.com
or https://gitlab.com
. Simply substitute username
by your own username in the repository hosting service. It's also worth noting that the code repository for the book, https://github.com/practicalgo/code
, contains the module path as github.com/practicalgo/code/<chap1>/<application-name>
, in other words, an actual path that exists rather than a placeholder path.
Command Line and Terminals
You will be required to execute command-line programs throughout the book. For Linux and macOS, the default terminal СКАЧАТЬ