Build Systems With Go Everything a Gopher must know (Juan M. Tirado) (Z-Library)

Author: Juan M. Tirado

科学

Everything a Gopher must know in a single book!!! Do not stay in the basics, move forward and learn how you can use Go to build systems using restful APIs, gRPC messaging, powerful loggers, middlware, SQL/noSQL databases, data streaming, and more. A book written for new adopters and experienced developers. More than 200 detailed examples Concise explanations from basic to advanced levels. Explore advanced topics: reflection, concurrency, benchmarking, profiling, etc. Learn how developers use Go: modules, tags, CLIs, encodings, etc. Learn how to build systems with gRPC, Kafka, Cassandra, MySQL, Cobra and more. Contents Part I: The GO language First steps with Go The basics Arrays, slices, and maps Structs, methods and interfaces Reflection Concurrency Input/Output Encodings HTTP Templates Testing Modules and documentation Part II: Building systems Protocol buffers gRPC Logging with Zerolog Command line interface Relational databases NoSQL databases Kafka The author Juan M. Tirado has been programming half of his life. He holds a Ph. D. in computer science and has been a researcher at the UC3M, INRIA, and the University of Cambridge. He is interested in how data can be leveraged to enhance large scale distributed systems. With a background between a systems architect and a data scientist, he helps companies to design and implement data-driven solutions. In his free time, he enjoys music, mountaineering, and tapas.

📄 File Format: PDF
💾 File Size: 3.8 MB
87
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
Build systems with Go Everything a Gopher must know Juan M. Tirado Build systems with Go by Juan M. Tirado Copyright ©2021 Independently published Cover by Juan M.Tirado Gopher Gotham image by Egon Elbre (@egonelbre) All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording or by any information storage or retrieval system, without the prior written permission of the copyright owner. This book has been entirely written using LATEX. EPub 3.0 conversion was done using tex4ebook: https://github.com/michal-h21/tex4ebook Revision History: v0.1.0: 2021-03-29 First version
📄 Page 3
PREFACE Welcome and thank you for reading these lines. Since I started programming in Go, I have always enjoyed its extraordinary commitment to simplicity. It is difficult to find another language that can make complex things so easily. That is the beauty of this language. Years have passed by and Go is no longer the new kid on the block, it has already become a mature language surrounded by a rich ecosystem of libraries, projects, and tools. Talking about Go is no longer talking about that fancy language that makes your life easier. Go is the gravity centre of a continuously growing ecosystem of amazing solutions maintained by a devoted community of developers. Go was originally designed to simplify the building of complex systems. However, when a developer decides to learn Go most of the learning resources simply explain the language. This book goes one step further by exploring tools, libraries, and projects from the Go ecosystem you can use to build ready-for-production systems. Everything a gopher must know in a single book. I hope you find this book useful. WHO SHOULD READ THIS BOOK? This book is oriented to new Go adopters and developers with programming experience in other languages. The first part of this book covers the Go language from its basics to more advanced concepts. The second part assumes these concepts to be known by the reader and explores how to use them with other tools to build systems. If you are new to Go you can start from the beginning. However, if you have some experience you can start with the second part and revisit any basic concept if needed. Or you can simply go and check the chapters at your convenience. STRUCTURE OF THIS BOOK
📄 Page 4
This book is structured to easily find those pieces you may find more interesting for your work. However, if you are an absolute beginner or you do not feel very comfortable with all the concepts explained in this book you can always start from the beginning. Whatever your use case is, these are the contents of this book. Part I: The GO language The first part explores the language from the very basics to advanced tools offered by the standard library. Chapter 1: First steps with Go This Chapter is specifically written to motivate newbies to run their first Go program. Chapter 2: The basics This Chapter explains all the Go basics including syntax, variables, types, pointers, functions, and execution flow. Chapter 3: Arrays, slices, and maps Go includes powerful native data structures such as arrays and maps. This Chapter extends previous concepts and shows the reader how to write her first data processing solutions. Chapter 4: Structs, methods, and interfaces This Chapter describes how Go defines advanced data structures, their associated methods, and interfaces. Chapter 5: Reflection By exploring how Go uses reflection, the reader can understand the many possibilities of manipulating in-memory data structures. Chapter 6: Concurrency Concurrency is not an easy topic. However, this Chapter demonstrates how Go help developers to design complex solutions effortless. This Chapter covers goroutines, channels, concurrency statements, contexts and more. Chapter 7: Input/Output Any program requires to write or read data to and from different
📄 Page 5
sources. This Chapter explains through examples how Go provides I/O support. Chapter 8: Encodings The Go standard library offers by default solutions to work with encodings such as CSV, JSON or XML. This Chapter, explains how to use these encodings and others not available by default. Chapter 9: HTTP This Chapter explains how we can implement our own HTTP clients and servers, and how to deal with requests, cookies, headers or middleware. Chapter 10: Templates Templates are pieces of data than can be filled programmatically. This Chapter explains how to define, customize, and use them. Chapter 11: Testing This Chapter will show the reader how simple it is to execute testing routines and benchmarks in Go. Additionally, it will introduce the reader how to run coverage tests and execution profiles. Chapter 12: Modules and documentation This Chapter explains how to manage dependencies in Go and how to document code. Part II: Building systems The second part of the book is oriented to those readers who feel comfortable with the language and want to explore solutions from the Go ecosystem that can be used to build sophisticated systems. Chapter 13: Protocol buffers This Chapter reviews what is the protocol buffer serialization format and how to use it with Go. Chapter 14: gRPC Read this Chapter if you need of a fast, modular, and easy-to-deploy message protocol in your system. This Chapter explains how to define services, servers, clients, streaming, and interceptors. Chapter 15: Logging with Zerolog
📄 Page 6
This Chapter shows the reader how to log a program using the powerful Zerolog library. Chapter 16: Command Line Interface Complex programs require complex command line interfaces. This Chapters, shows the developer how to define and integrate the Cobra library in their projects to obtain professional CLIs with minimal effort. Chapter 17: Relational databases This Chapter introduces how the standard library can be used to manipulate and query data from SQL databases. Additionally, it explores how to use the GORM library for ORM solutions. Chapter 18: NoSQL databases NoSQL database solutions are quite common and the Go ecosystem offers solutions to work with them. This Chapter, explains how to operate with Apache Cassandra using the GoCQL client. Chapter 19: Kafka This Chapter reviews the basics of Apache Kafka and overviews three different solutions to interact with this streaming platform. CONVENTIONS This book is built around self-contained examples. These examples are minimalist pieces of code that help the reader becoming familiar with the explained concepts. Examples are small enough to bring the reader an idea of how a real program looks like. Some examples may print something to help the reader, in that case, the expected output is shown for the reader’s convenience. This is how an example looks like. : Title of this example. 1 In the left side 2 of this box, 3 you can find
📄 Page 7
4 the code for 5 this example The output goes here. Additional tips and notes can be found across the book. This is a warning note. This is a curiosity or tip with additional information. THE CODE This book contains a large number of examples fully available at the author’s GitHub repository under the Apache license: https://github.com/juanmanuel-tirado/savetheworldwithgo Feel free to fork the repository at your convenience. If you find any issue or have any comment regarding the code, please let the author know. ABOUT THE AUTHOR Juan M. Tirado has been programming half of his life. He holds a Ph. D. in computer science and has been a researcher at the UC3M, INRIA, and the University of Cambridge. He is interested in how data can be leveraged to enhance large scale distributed systems. With a background between a systems architect and a data scientist, he helps companies to design and implement data-driven solutions. In his free time, he enjoys music, mountaineering, and tapas. You can follow the author at: Website: https://jmtirado.net/ LinkedIn: https://www.linkedin.com/in/juan-tirado Medium: https://juanmanuel-tirado.medium.com/ Twitter: @jmtirado
📄 Page 8
SOME WORDS OF GRATITUDE This book is a one-person project carried out with a lot of effort and great illusion. If you have found this book useful, the author would appreciate you spread the word and tell your friends and colleagues. Your comments and/or suggestions are always welcome to help in improving this book.
📄 Page 9
Part I The GO language
📄 Page 10
CHAPTER 1 FIRST STEPS WITH GO Save the world with Go!!! This chapter will show you how to write, compile and execute your first program in Go. For this, you need a working Go installation. Follow the steps for your platform described in the official documentation1. Next, take any plain text editor of your choice: NotePad, Nano, Vim, etc. You will need one of them to write down the code. If you prefer to use more sophisticated tools such as GoLand, Atom or Visual Studio Code the following examples still apply. However, I recommend you follow the current explanation if this is your first time with Go. 1.1 SAVE THE WORLD WITH GO!!! If you are familiar with any computer language you already know what comes next: a Hello World! program. This is just a program that will print a message in your console output. Traditionally this is the first approach to any programming language. And this is still the case although we have changed the message. Example 1.1: Save the world with Go!!! 1 package main 2 3 import “fmt” 4 5 func main() { 6 fmt.Println(“Save the world with Go!!!”) 7 } The above code has the basic components of a Go program. First, we set the name of the package that contains our code (line 1). In line 2, we import the library required to invoke our Println function. The logic of our program is contained between brackets in a function called main between lines 5 and 7. The statement in line 6 prints our message using the standard output. Go must be compiled before execution. This is, we need to run our code through a compiler to
📄 Page 11
generate executable code for our platform. The result from the compilation process is an executable file. Depending on your platform this file will be different. To compile our program, we only need to write down the code above in any text editor, save it as main.go and compile it. To compile the code only run the go build command. Example 1.2: Compilation with go build. >> go build main.go >> ls main main.go >> ./main Save the world with Go!!! If you run the code above in a Unix-compatible terminal you should get the same result. As you can see, the process is straight forward for this example. The go build command generates an executable file named main. This file can be executed (notice that ./ runs any executable file) displaying our message. 1.2 PASSING ARGUMENTS TO OUR PROGRAM Now that we already know how to print a message, it would be nice if we could add some information from the outside. For example, what about computing the sum of two numbers? The idea is to pass two numbers to our program and tell the user what is the resulting sum. First, we need to know how we can pass arguments to our program. This can be done using the os.Args variable. The example below is taken from here2. Example 1.3: Passing arguments. 1 package main 2 3 import ( 4 “fmt” 5 “os”
📄 Page 12
6 ) 7 8 func main() { 9 10 argsWithProg := os.Args 11 argsWithoutProg := os.Args[1:] 12 13 arg := os.Args[3] 14 15 fmt.Println(argsWithProg) 16 fmt.Println(argsWithoutProg) 17 fmt.Println(arg) 18 } There is a bunch of interesting things in this code. We have declared and initialized three variables called argsWithProg,argsWithoutProg, and arg. These variables contain all the arguments passed to our program, the arguments without the program name, and the argument in the third position respectively. If we compile and run the program like shown in the previous example we can understand how arguments passing works. Example 1.4: Passing arguments output >>> ./main Save the world with Go [./main Save the world with Go] [Save the world with Go] world The os.Args method returns an array (do not worry, this is explained in Chapter 3) containing all the arguments passed to the program including the name of the executable file. The variable ArgsWithoutProg has our input message (Save the world with Go). We removed the name of the program with the index os.Args[1:]. As mentioned before, this will be explained in more detail in the corresponding Chapter. In Go, arrays are indexed from 0 to with the array length. Finally, in
📄 Page 13
corresponding Chapter. In Go, arrays are indexed from 0 to with the array length. Finally, in arg we get the argument at position 3 returning the word world.
📄 Page 14
Now that we explored how we can pass arguments to a program, we can do something with these parameters. Example 1.5: Sum two numbers passed by arguments. 1 package main 2 3 import ( 4 “fmt” 5 “os” 6 “strconv” 7 ) 8 9 func main() { 10 11 argsWithProg := os.Args 12 13 numA, err := strconv.Atoi(argsWithProg[1]) 14 if err != nil { 15 fmt.Println(err) 16 os.Exit(2) 17 } 18 numB, err := strconv.Atoi(argsWithProg[2]) 19 if err != nil { 20 fmt.Println(err) 21 os.Exit(2) 22 } 23 result := numA + numB 24 fmt.Printf(“%d + %d = %d\n”, numA, numB, result) 25 }
📄 Page 15
We can only run mathematical operations with numbers. This is a problem because arguments are passed as strings of characters. Fortunately, we can use the strconv.Atoi function to convert an integer number into a string representation. This may result in some conversion errors. For example: “42” 42
📄 Page 16
“42” 42 “-33” -33
📄 Page 17
“-33” -33 “4.2” This is a conversion error because we are not expecting floating numbers.
📄 Page 18
“4.2” This is a conversion error because we are not expecting floating numbers. “thirteen” This is a conversion error because this is a textual representation of a number.
📄 Page 19
“thirteen” This is a conversion error because this is a textual representation of a number. Is for this reason that strconv.Atoi returns two parameters. The first one is the integer number we can extract from the string. The second one is an error variable that will be filled in case there is an error. To know if there was a problem during the conversion process we can check if the error variable was filled or not. This is done in lines 14 and 19 with if statements. If the err variable contains some value (!=nil), we print the error and exit the program with os.Exit(2). If everything is correct, we compute the sum of numA and numB variables and print the result. To make it more appealing, we add some additional formatting to our output in line 24. You do not need to fully understand the meaning of fmt.Printf but you can guess that we are filling a string with numA, numB, and result values. Now we can compile it and run like we did before: Example 1.6: Sum numbers output. >>> ./sum 2 2 2 + 2 = 4 >>> ./sum 42 -2 42 + -2 = 40 >>> ./sum 2 2.2 strconv.Atoi: parsing “2.2”: invalid syntax >>> ./sum 2 two strconv.Atoi: parsing “two”: invalid syntax And voilà! our little calculator is ready. We can sum two numbers and detect when the input cannot be converted into an integer. However, there is one potential issue? What happens if we do not have any arguments? Consider this as an improvement exercise. 1.3 SUMMARY
📄 Page 20
In this Chapter, we showed how to write a Go program, compile it, and execute it. Additionally, we extended a basic example to include arguments passing and perform some mathematical operations, error control, and mathematical operations. If you feel comfortable with the content of this Chapter, consider exploring the basics of Go as presented in Chapter 2 .
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List