logo资料库

mastering go packt 2018.pdf

第1页 / 共933页
第2页 / 共933页
第3页 / 共933页
第4页 / 共933页
第5页 / 共933页
第6页 / 共933页
第7页 / 共933页
第8页 / 共933页
资料共933页,剩余部分请下载后查看
Title Page
Copyright and Credits
Mastering Go
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Go and the Operating System
The structure of the book
The history of Go
Why learn Go?
Go advantages
Is Go perfect?
What is a preprocessor?
The godoc utility
Compiling Go code
Executing Go code
Two Go rules
You either use a Go package or do not include it
There is only one way to format curly braces
Downloading Go packages
Unix stdin, stdout, and stderr
About printing output
Using standard output
Getting user input
About := and =
Reading from standard input
Working with command-line arguments
About error output
Writing to log files
Logging levels
Logging facilities
Log servers
A Go program that sends information to log files
About log.Fatal()
About log.Panic()
Error handling in Go
The error data type
Error handling
Additional resources
Exercises
Summary
Understanding Go Internals
The Go compiler
Garbage Collection
The Tricolor algorithm
More about the operation of the Go Garbage Collector
Unsafe code
About the unsafe package
Another example of the unsafe package
Calling C code from Go
Calling C code from Go using the same file
Calling C code from Go using separate files
The C code
The Go code
Mixing Go and C code
Calling Go functions from C code
The Go package
The C code
The defer keyword
Panic and Recover
Using the panic function on its own
Two handy Unix utilities
The strace tool
The dtrace tool
Your Go environment
The Go Assembler
Node Trees
Learning more about go build
General Go coding advices
Additional Resources
Exercises
Summary
Working with Basic Go Data Types
Go loops
The for loop
The while loop
The range keyword
Examples of Go for loops
Go arrays
Multi-dimensional arrays
The shortcomings of Go arrays
Go slices
Performing basic operations on slices
Slices are being expanded automatically
Byte slices
The copy() function
Multidimensional slices
Another example of slices
Sorting slices using sort.slice()
Go maps
Storing to a nil map
When you should use a map?
Go constants
The constant generator iota
Go pointers
Dealing with times and dates
Working with times
Parsing times
Working with dates
Parsing dates
Changing date and time formats
Additional resources
Exercises
Summary
The Uses of Composite Types
About composite types
Structures
Pointers to structures
Using the new keyword
Tuples
Regular expressions and pattern matching
Now for some theory
A simple example
A more advanced example
Matching IPv4 addresses
Strings
What is a rune?
The Unicode package
The strings package
The switch statement
Calculating Pi with great accuracy
Developing a key/value store in Go
Additional resources
Exercises
Summary
Enhancing Go Code with Data Structures
About graphs and nodes
Algorithm complexity
Binary trees in Go
Implementing a binary tree in Go
Advantages of binary trees
Hash tables in Go
Implementing a hash table in Go
Implementing the lookup functionality
Advantages of hash tables
Linked lists in Go
Implementing a linked list in Go
Advantages of linked lists
Doubly linked lists in Go
Implementing a doubly linked list in Go
Advantages of doubly linked lists
Queues in Go
Implementing a queue in Go
Stacks in Go
Implementing a stack in Go
The container package
Using container/heap
Using container/list
Using container/ring
Generating random numbers
Generating random strings
Additional Resources
Exercises
Summary
What You Might Not Know About Go Packages
About Go packages
About Go functions
Anonymous functions
Functions that return multiple values
The return values of a function can be named!
Functions with pointer parameters
Functions that return pointers
Functions that return other functions
Functions that accept other functions as parameters
Developing your own Go packages
Compiling a Go package
Private variables and functions
The init() function
Reading the Go code of a standard Go package
Exploring the code of the net/url package
Looking at the Go code of the log/syslog package
Creating good Go packages
The syscall package
Finding out how fmt.Println() really works
Text and HTML templates
Generating text output
Constructing HTML output
Basic SQLite3 commands
Additional resources
Exercises
Summary
Reflection and Interfaces for All Seasons
Type methods
Go interfaces
About type assertion
Developing your own interfaces
Using a Go interface
Using switch with interface and data types
Reflection
A simple Reflection example
A more advanced reflection example
The three disadvantages of reflection
Object-oriented programming in Go!
Additional resources
Exercises
Summary
Telling a Unix System What to Do
About Unix processes
The flag package
The io.Reader and io.Writer interfaces
Buffered and unbuffered file input and output
The bufio package
Reading text files
Reading a text file line by line
Reading a text file word by word
Reading a text file character by character
Reading from /dev/random
Reading the amount of data you want from a file
Why are we using binary format?
Reading CSV files
Writing to a file
Loading and saving data on disk
The strings package revisited
About the bytes package
File permissions
Handling Unix signals
Handling two signals
Handling all signals
Programming Unix pipes in Go
Implementing the cat(1) utility in Go
Traversing directory trees
Using eBPF from Go
About syscall.PtraceRegs
Tracing system calls
User ID and group ID
Additional resources
Exercises
Summary
Go Concurrency – Goroutines, Channels, and Pipelines
About processes, threads, and goroutines
The Go scheduler
Concurrency and parallelism
Goroutines
Creating a goroutine
Creating multiple goroutines
Waiting for your goroutines to finish
What if the number of Add() and Done() calls do not agree?
Channels
Writing to a channel
Reading from a channel
Channels as function parameters
Pipelines
Additional resources
Exercises
Summary
Go Concurrency – Advanced Topics
The Go scheduler revisited
The GOMAXPROCS environment variable
The select keyword
Timing out a goroutine
Timing out a goroutine – take 1
Timing out a goroutine – take 2
Go channels revisited
Signal channels
Buffered channels
Nil channels
Channel of channels
Specifying the order of execution for your goroutines
Shared memory and shared variables
The sync.Mutex type
What happens if you forget to unlock a mutex?
The sync.RWMutex type
Sharing memory using goroutines
Catching race conditions
The context package
An advanced example of the context package
Worker pools
Additional resources
Exercises
Summary
Code Testing, Optimization, and Profiling
The Go version used in this chapter
Comparing Go version 1.10 with Go version 1.9
Installing a beta or RC version of Go
About optimization
Optimizing Go code
Profiling Go code
The net/http/pprof standard Go package
A simple profiling example
A convenient external package for profiling
The web interface of the Go profiler
A profiling example that uses the web interface
A quick introduction to Graphviz
The go tool trace utility
Testing Go code
Writing tests for existing Go code
Benchmarking Go code
A simple benchmarking example
A wrong benchmark function
Benchmarking buffered writing
Finding unreachable Go code
Cross-compilation
Creating example functions
Generating documentation
Additional resources
Exercises
Summary
The Foundations of Network Programming in Go
About net/http, net, and http.RoundTripper
The http.Response type
The http.Request type
The http.Transport type
About TCP/IP
About IPv4 and IPv6
The nc(1) command-line utility
Reading the configuration of network interfaces
Performing DNS lookups
Getting the NS records of a domain
Getting the MX records of a domain
Creating a web server in Go
Profiling an HTTP server
Creating a website in Go
HTTP tracing
Testing HTTP handlers
Creating a web client in Go
Making your Go web client more advanced
Timing out HTTP connections
More information about SetDeadline
Setting the timeout period on the server side
Yet another way to time out!
Wireshark and tshark tools
Additional resources
Exercises
Summary
Network Programming – Building Servers and Clients
The net standard Go package
A TCP client
A slightly different version of the TCP client
A TCP server
A slightly different version of the TCP server
A UDP client
Developing a UDP server
A concurrent TCP server
A handy concurrent TCP server
Remote Procedure Call (RPC)
The RPC client
The RPC server
Doing low-level network programming
Grabbing raw ICMP network data
Where to go next?
Additional resources
Exercises
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Download from finelybook 7450911@qq.com Mastering Go Create Golang production applications using network libraries, concurrency, and advanced Go data structures 2
Download from finelybook 7450911@qq.com Mihalis Tsoukalos BIRMINGHAM - MUMBAI 3
Download from finelybook 7450911@qq.com Mastering Go Copyright © 2018 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Acquisition Editors: Frank Pohlmann, Suresh Jain Project Editor: Kishor Rit Content Development Editor: Gary Schwarts Technical Editors: Gaurav Gavas, Nidhisha Shetty Proofreader: Tom Jacob Indexer: Mariammal Chettiyar Graphics: Tom Scaria Production Coordinator: Shantanu Zagade First published: April 2018 Production reference: 1270418 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78862-654-5 www.packtpub.com 4
Download from finelybook 7450911@qq.com Packt Upsell mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. 5
Download from finelybook 7450911@qq.com Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content 6
Download from finelybook 7450911@qq.com 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. 7
Download from finelybook 7450911@qq.com Contributors 8
分享到:
收藏