0%

Book Description

Go was created by Robert Griesemer, Rob Pike, and Ken Thompson to be an expressive and efficient language for writing readable and robust programs. Since its announcement in 2009, users of traditional, compiled languages have found Go’s simplicity, high-quality libraries, and straightforward tools to be a refreshing change. Go has also been winning converts from users of dynamic languages—those who appreciate how its lightweight type system makes their code safer and faster.

The Go Programming Language is the authoritative resource for any programmer who wants to learn Go quickly and effectively. It assumes no prior knowledge of Go nor prior experience with any specific language, so it provides a solid foundation whether your previous experience is with JavaScript, Ruby, Python, Java, or C++. This guide will show you how to write efficient and idiomatic Go, making good use of its standard libraries to solve real-world problems.

  • The first chapter is a tutorial on the basic constructs of Go, introduced through example programs for file I/O and text processing, simple graphics, and web clients and servers.

  • Early chapters cover the structural elements of a Go program: its syntax, basic and composite data types, functions, and error handling. The examples illustrate many standard packages, how to create new ones, and how to build, test, and manage projects using the go tool.

  • The chapters on methods and interfaces introduce Go’s unusual approach to object-oriented programming, including the key principles of encapsulation and composition, as well as Go’s distinctive notion of implicitly satisfied interfaces.

  • Two chapters on concurrency present in-depth approaches to this important topic. The first covers the basic mechanisms of goroutines and channels and the style of communicating sequential processes for which Go is renowned. The second covers more traditional aspects of using concurrency with shared variables. These chapters assume no prior knowledge of concurrency, so they provide an excellent tutorial for programmers who would like to understand it better.

  • The final two chapters explore lower-level features of Go. One covers the art of metaprogramming using reflection. The other shows how to use the unsafe package to step outside the type system in a controlled way for special situations, and how to use the cgo tool to create Go bindings for C libraries.

  • The book features hundreds of interesting and practical examples of idiomatic Go code that cover the whole language, its most important libraries, and a wide range of applications. Source code will be freely available for download from the book's companion website (upon publication) and may be conveniently fetched, built, and installed using the go get command.

    Table of Contents

    1. Contents
    2. Preface
      1. The Origins of Go
      2. The Go Project
      3. Organization of the Book
      4. Where to Find More Information
      5. Acknowledgments
    3. 1. Tutorial
      1. 1.1 Hello, World
      2. 1.2 Command-Line Arguments
      3. 1.3 Finding Duplicate Lines
      4. 1.4 Animated GIFs
      5. 1.5 Fetching a URL
      6. 1.6 Fetching URLs Concurrently
      7. 1.7 A Web Server
      8. 1.8 Loose Ends
    4. 2. Program Structure
      1. 2.1 Names
      2. 2.2 Declarations
      3. 2.3 Variables
      4. 2.4 Assignments
      5. 2.5 Type Declarations
      6. 2.6 Packages and Files
      7. 2.7 Scope
    5. 3. Basic Data Types
      1. 3.1 Integers
      2. 3.2 Floating-Point Numbers
      3. 3.3 Complex Numbers
      4. 3.4 Booleans
      5. 3.5 Strings
      6. 3.6 Constants
    6. 4. Composite Types
      1. 4.1 Arrays
      2. 4.2 Slices
      3. 4.3 Maps
      4. 4.4 Structs
      5. 4.5 JSON
      6. 4.6 Text and HTML Templates
    7. 5. Functions
      1. 5.1 Function Declarations
      2. 5.2 Recursion
      3. 5.3 Multiple Return Values
      4. 5.4 Errors
      5. 5.5 Function Values
      6. 5.6 Anonymous Functions
      7. 5.7 Variadic Functions
      8. 5.8 Deferred Function Calls
      9. 5.9 Panic
      10. 5.10 Recover
    8. 6. Methods
      1. 6.1 Method Declarations
      2. 6.2 Methods with a Pointer Receiver
      3. 6.3 Composing Types by Struct Embedding
      4. 6.4 Method Values and Expressions
      5. 6.5 Example: Bit Vector Type
      6. 6.6 Encapsulation
    9. 7. Interfaces
      1. 7.1 Interfaces as Contracts
      2. 7.2 Interface Types
      3. 7.3 Interface Satisfaction
      4. 7.4 Parsing Flags with flag.Value
      5. 7.5 Interface Values
      6. 7.6 Sorting with sort.Interface
      7. 7.7 The http.Handler Interface
      8. 7.8 The error Interface
      9. 7.9 Example: Expression Evaluator
      10. 7.10 Type Assertions
      11. 7.11 Discriminating Errors with Type Assertions
      12. 7.12 Querying Behaviors with Interface Type Assertions
      13. 7.13 Type Switches
      14. 7.14 Example: Token-Based XML Decoding
      15. 7.15 A Few Words of Advice
    10. 8. Goroutines and Channels
      1. 8.1 Goroutines
      2. 8.2 Example: Concurrent Clock Server
      3. 8.3 Example: Concurrent Echo Server
      4. 8.4 Channels
      5. 8.5 Looping in Parallel
      6. 8.6 Example: Concurrent Web Crawler
      7. 8.7 Multiplexing with select
      8. 8.8 Example: Concurrent Directory Traversal
      9. 8.9 Cancellation
      10. 8.10 Example: Chat Server
    11. 9. Concurrency with Shared Variables
      1. 9.1 Race Conditions
      2. 9.2 Mutual Exclusion: sync.Mutex
      3. 9.3 Read/Write Mutexes: sync.RWMutex
      4. 9.4 Memory Synchronization
      5. 9.5 Lazy Initialization: sync.Once
      6. 9.6 The Race Detector
      7. 9.7 Example: Concurrent Non-Blocking Cache
      8. 9.8 Goroutines and Threads
    12. 10. Packages and the Go Tool
      1. 10.1 Introduction
      2. 10.2 Import Paths
      3. 10.3 The Package Declaration
      4. 10.4 Import Declarations
      5. 10.5 Blank Imports
      6. 10.6 Packages and Naming
      7. 10.7 The Go Tool
    13. 11. Testing
      1. 11.1 The go test Tool
      2. 11.2 Test Functions
      3. 11.3 Coverage
      4. 11.4 Benchmark Functions
      5. 11.5 Profiling
      6. 11.6 Example Functions
    14. 12. Reflection
      1. 12.1 Why Reflection?
      2. 12.2 reflect.Type and reflect.Value
      3. 12.3 Display, a Recursive Value Printer
      4. 12.4 Example: Encoding S-Expressions
      5. 12.5 Setting Variables with reflect.Value
      6. 12.6 Example: Decoding S-Expressions
      7. 12.7 Accessing Struct Field Tags
      8. 12.8 Displaying the Methods of a Type
      9. 12.9 A Word of Caution
    15. 13. Low-Level Programming
      1. 13.1 unsafe.Sizeof, Alignof, and Offsetof
      2. 13.2 unsafe.Pointer
      3. 13.3 Example: Deep Equivalence
      4. 13.4 Calling C Code with cgo
      5. 13.5 Another Word of Caution
    16. Index