- English
- ePUB (mobile friendly)
- Available on iOS & Android
Go Standard Library Cookbook
About This Book
Implement solutions by leveraging the power of the GO standard library and reducing dependency on external cratesAbout This Bookā¢ Develop high quality, fast and portable applications by leveraging the power of Go Standard Library.ā¢ Practical recipes that will help you work with the standard library algorithms to boost your productivity as a Go developer.ā¢ Compose your own algorithms without forfeiting the simplicity and elegance of the Standard Library.Who This Book Is ForThis book is for Go developers who would like to explore the power of Golang and learn how to use the Go standard library for various functionalities. The book assumes basic Go programming knowledge.What You Will Learnā¢ Access environmental variablesā¢ Execute and work with child processesā¢ Manipulate strings by performing operations such as search, concatenate, and so onā¢ Parse and format the output of date/time informationā¢ Operate on complex numbers and effective conversions between different number formats and basesā¢ Work with standard input and outputā¢ Handle filesystem operations and file permissionsā¢ Create TCP and HTTP servers, and access those servers with a clientā¢ Utilize synchronization primitivesā¢ Test your codeIn DetailGoogle's Golang will be the next talk of the town, with amazing features and a powerful library. This book will gear you up for using golang by taking you through recipes that will teach you how to leverage the standard library to implement a particular solution. This will enable Go developers to take advantage of using a rock-solid standard library instead of third-party frameworks.The book begins by exploring the functionalities available for interaction between the environment and the operating system. We will explore common string operations, date/time manipulations, and numerical problems.We'll then move on to working with the database, accessing the filesystem, and performing I/O operations. From a networking perspective, we will touch on client and server-side solutions. The basics of concurrency are also covered, before we wrap up with a few tips and tricks.By the end of the book, you will have a good overview of the features of the Golang standard library and what you can achieve with them. Ultimately, you will be proficient in implementing solutions with powerful standard libraries.Style and approachSolution based approach showcasing the power of Go standard library for easy practical implementations.
Frequently asked questions
Information
Interacting with the Environment
- Retrieving the Golang version
- Accessing program arguments
- Creating a program interface with the flag package
- Getting and setting environment variables with default values
- Retrieving the current working directory
- Getting the current process PID
- Handling operating system signals
- Calling an external process
- Retrieving child process information
- Reading/writing from the child process
- Shutting down the application gracefully
- File configuration with functional options
Introduction
Retrieving the Golang version
Getting ready
- Download and install Go on your machine.
- Verify that your GOPATH and GOROOT environmental variables are set properly.
- Open your Terminal and execute go version. If you get output with a version name, then Go is installed properly.
- Create a repository in the GOPATH/src folder.
How to do it...
- Open the console and create the folder chapter01/recipe01.
- Navigate to the directory.
- Create the main.go file with the following content:
package main
import (
"log"
"runtime"
)
const info = `
Application %s starting.
The binary was build by GO: %s`
func main() {
log.Printf(info, "Example", runtime.Version())
}
- Run the code by executing the go run main.go.
- See the output in the Terminal:
How it works...
Accessing program arguments
How to do it...
- Open the console and create the folder chapter01/recipe02.
- Navigate to the directory.
- Create the main.go file with the following content:
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
// This call will print
// all command line arguments.
fmt.Println(args)
// The first argument, zero item from slice,
// is the name of the called binary.
programName := args[0]
fmt.Printf("The binary name is: %s \n", programName)...
Table of contents
- Title Page
- Copyright and Credits
- Packt Upsell
- Contributors
- Preface
- Interacting with the Environment
- Strings and Things
- Dealing with Numbers
- Once Upon a Time
- In and Out
- Discovering the Filesystem
- Connecting the Network
- Working with Databases
- Come to the Server Side
- Fun with Concurrency
- Tips and Tricks
- Other Books You May Enjoy