Practical Go. Amit Saha

Читать онлайн.
Название Practical Go
Автор произведения Amit Saha
Жанр Программы
Серия
Издательство Программы
Год выпуска 0
isbn 9781119773832



Скачать книгу

      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 :

      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.

      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.

      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 program running your default shell is sufficient. For Windows, I assume that you will be using the Windows PowerShell terminal instead of the default command-line program. Most of the command-line executions are shown as executed on a Linux/macOS terminal, indicated by the $ symbol. However, you also should be able to run the same command on Windows. Wherever I have asked you to execute a command to create a directory or copy a file, I have indicated the commands for both Linux/macOS and Windows, where they are different.

      Terms

      I have used some terms throughout the book that may be best clarified here to avoid ambiguity and set the right expectations.

       Robustness and Resiliency

      Both terms, robustness and resiliency, express the ability of an application to handle unexpected scenarios. However, these terms differ in their expected behavior under these circumstances as compared to their normal behavior. A system is robust if it can withstand unexpected situations and continue to function to some degree. This will likely be suboptimal behavior, as compared to normal behavior. On the other hand, a system is resilient if it continues exhibiting its normal behavior, potentially taking a finite amount of time before being able to do so. I put forward the following examples from the book to illustrate the difference.

      In Chapter 2, you will learn to enforce time-outs for command-line application functionality that is executing a user-specified program. By enforcing time-outs, we avoid the scenario where the application continues to hang indefinitely because of bad user output. Since we configure an upper bound on how long we want to allow the user-specified command to be executed, we will exit with an error when this duration expires before the command could be completed. This is not the normal behavior of the application—that we should wait for the command to complete—but this suboptimal behavior is necessary to allow the application to recover from an unexpected situation,