logo资料库

Go Cookbook [pdf].pdf

第1页 / 共392页
第2页 / 共392页
第3页 / 共392页
第4页 / 共392页
第5页 / 共392页
第6页 / 共392页
第7页 / 共392页
第8页 / 共392页
资料共392页,剩余部分请下载后查看
Cover
Copyright
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Table of Contents
Preface
Chapter 1: I/O and File Systems
Introduction
Using the common I/O interfaces
Getting ready
How to do it...
How it works...
Using the bytes and strings packages
Getting ready
How to do it...
How it works...
Working with directories and files
Getting ready
How to do it...
How it works...
Working with the CSV format
Getting ready
How to do it...
How it works...
Working with temporary files
Getting ready
How to do it...
How it works...
Working with text/template and HTML/templates
Getting ready
How to do it...
How it works...
Chapter 2: Command-Line Tools
Introduction
Using command-line flags
Getting ready
How to do it...
How it works...
Using command-line arguments
Getting ready
How to do it...
How it works...
Reading and setting environment variables
Getting ready
How to do it...
How it works...
Configuration using TOML, YAML, and JSON
Getting ready
How to do it...
How it works...
Working with Unix pipes
Getting ready
How to do it...
How it works...
Catching and handling signals
Getting ready
How to do it...
How it works...
An ANSI coloring application
Getting ready
How to do it...
How it works...
Chapter 3: Data Conversion and Composition
Introduction
Converting data types and interface casting
Getting ready
How to do it...
How it works...
Working with numeric data types using math and math/big
Getting ready
How to do it...
How it works...
Currency conversions and float64 considerations
Getting ready
How to do it...
How it works...
Using pointers and SQL NullTypes for encoding and decoding
Getting ready
How to do it...
How it works...
Encoding and decoding Go data
Getting ready
How to do it...
How it works...
Struct tags and basic reflection in Go
Getting ready
How to do it...
How it works...
Implementing collections via closures
Getting ready
How to do it...
How it works...
Chapter 4: Error Handling in Go
Introduction
Handling errors and the Error interface
Getting ready
How to do it...
How it works...
Using the pkg/errors package and wrapping errors
Getting ready
How to do it...
How it works...
Using the log package and understanding when to log errors
Getting ready
How to do it...
How it works...
Structured logging with the apex and logrus packages
Getting ready
How to do it...
How it works...
Logging with the context package
Getting ready
How to do it...
How it works...
Using package-level global variables
Getting ready
How to do it...
How it works...
Catching panics for long running processes
Getting ready
How to do it...
How it works...
Chapter 5: All about Databases and Storage
Introduction
The database/sql package with MySQL
Getting ready
How to do it...
How it works...
Executing a database transaction interface
Getting ready
How to do it...
How it works...
Connection pooling, rate limiting, and timeouts for SQL
Getting ready
How to do it...
How it works...
Working with Redis
Getting ready
How to do it...
How it works...
Using NoSQL with MongoDB and mgo
Getting ready
How to do it...
How it works...
Creating storage interfaces for data portability
Getting ready
How to do it...
How it works...
Chapter 6: Web Clients and APIs
Introduction
Initializing, storing, and passing http.Client structs
Getting ready
How to do it...
How it works...
Writing a client for a REST API
Getting ready
How to do it...
How it works...
Executing parallel and async client requests
Getting ready
How to do it...
How it works...
Making use of OAuth2 clients
Getting ready
How to do it...
How it works...
Implementing an OAuth2 token storage interface
Getting ready
How to do it...
How it works...
Wrapping a client in added functionality and function composition
Getting ready
How to do it...
How it works...
Understanding GRPC clients
Getting ready
How to do it...
How it works...
Chapter 7: Microservices for Applications in Go
Introduction
Working with web handlers, requests, and ResponseWriters
Getting ready
How to do it...
How it works...
Using structs and closures for stateful handlers
Getting ready
How to do it...
How it works...
Validating input for Go structs and user inputs
Getting ready
How to do it...
How it works...
Rendering and content negotiation
Getting ready
How to do it...
How it works...
Implementing and using middleware
Getting ready
How to do it...
How it works...
Building a reverse proxy application
Getting ready
How to do it...
How it works...
Exporting GRPC as a JSON API
Getting ready
How to do it...
How it works...
Chapter 8: Testing
Introduction
Mocking using the standard library
Getting ready
How to do it...
How it works...
Using the Mockgen package
Getting ready
How to do it...
How it works...
Using table-driven tests to improve coverage
Getting ready
How to do it...
How it works...
Using third-party testing tools
Getting ready
How to do it...
How it works...
Practical fuzzing
Getting ready
How to do it...
How it works...
Behavior testing using Go
Getting ready
How to do it...
How it works...
Chapter 9: Parallelism and Concurrency
Introduction
Using channels and the select statement
Getting ready
How to do it...
How it works...
Performing async operations with sync.WaitGroup
Getting ready
How to do it...
How it works...
Using atomic operations and mutex
Getting ready
How to do it...
How it works...
Using the context package
Getting ready
How to do it...
How it works...
Executing state management for channels
Getting ready
How to do it...
How it works...
Using the worker pool design pattern
Getting ready
How to do it...
How it works...
Using workers to create pipelines
Getting ready
How to do it...
How it works...
Chapter 10: Distributed Systems
Introduction
Using service discovery with Consul
Getting ready
How to do it...
How it works...
Implementing basic consensus using Raft
Getting ready
How to do it...
How it works...
Using containerization with Docker
Getting ready
How to do it...
How it works...
Orchestration and deployment strategies
Getting ready
How to do it...
How it works...
Monitoring applications
Getting ready
How to do it...
How it works...
Collecting metrics
Getting ready
How to do it...
How it works...
Chapter 11: Reactive Programming and Data Streams
Introduction
Goflow for dataflow programming
Getting ready
How to do it...
How it works...
Reactive programming with RxGo
Getting ready
How to do it...
How it works...
Using Kafka with Sarama
Getting ready
How to do it...
How it works...
Using async producers with Kafka
Getting ready
How to do it...
How it works...
Connecting Kafka to Goflow
Getting ready
How to do it...
How it works...
Writing a GraphQL server in Go
Getting ready
How to do it...
How it works...
Chapter 12: Serverless Programming
Introduction
Go programming on Lambda with Apex
Getting ready
How to do it...
How it works...
Apex serverless logging and metrics
Getting ready
How to do it...
How it works...
Google App Engine with Go
Getting ready
How to do it...
How it works...
Working with Firebase using zabawaba99/firego
Getting ready
How to do it...
How it works...
Chapter 13: Performance Improvements, Tips, and Tricks
Introduction
Speeding up compilation and testing cycles
Getting ready
How to do it...
How it works...
Using the pprof tool
Getting ready
How to do it...
How it works...
Benchmarking and finding bottlenecks
Getting ready
How to do it...
How it works...
Memory allocation and heap management
Getting ready
How to do it...
How it works...
Vendoring and project layout
Getting ready
How to do it...
How it works...
Using fasthttprouter and fasthttp
Getting ready
How to do it...
How it works...
Index
Go Cookbook Build modular, readable, and testable applications in Go Aaron Torres BIRMINGHAM - MUMBAI
Go Cookbook Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2017 Production reference: 1240617 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78328-683-6 www.packtpub.com
Credits Author Aaron Torres Reviewer Julien Da Silva Copy Editor Pranjali Chury Project Coordinator Vaidehi Sawant Commissioning Editor Kunal Parikh Proofreader Safis Editing Acquisition Editor Karan Sadawana Indexer Tejal Daruwale Soni Content Development Editor Rohit Kumar Singh Graphics Jason Monteiro Technical Editor Vivek Pala Production Coordinator Nilesh Mohite
About the Author Aaron Torres received his master’s of science degree in computer science from New Mexico Institute of Mining and Technology. He has worked on distributed systems in high performance computing and in large-scale web and microservices applications. He currently leads a team of Go developers that refines and focuses on Go best practices with an emphasis on continuous delivery and automated testing. Aaron has published a number of papers and has several patents in the area of storage and I/O. He is passionate about sharing his knowledge and ideas with others. He is also a huge fan of the Go language and open source for backend systems and development.
About the Reviewer Julien Da Silva is a software engineer and architect specializing in scalable, distributed systems. Previously at Hailo, he was a part of the team that built its Golang platform, which is widely recognized as one of the early successful implementations of microservices. Hailo was later acquired by MyTaxi in 2016. He is currently working as a core architect at LastMileLink, part of CitySprint group, UK leader in same day courier services.
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /G o - C o o k b o o k - A a r o n - T o r r e s - e b o o k /d p /B 01M S 2M O F P /r e f =s r _ 1_ 16?i e =U T F 8&q i d =1497940919&s r =8- 16&k e y w o r d s =g o +c o o k b o o k . If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
分享到:
收藏