Go Systems Programming

Book description

Learning the new system’s programming language for all Unix-type systems

About This Book

  • Learn how to write system's level code in Golang, similar to Unix/Linux systems code
  • Ramp up in Go quickly
  • Deep dive into Goroutines and Go concurrency to be able to take advantage of Go server-level constructs

Who This Book Is For

Intermediate Linux and general Unix programmers. Network programmers from beginners to advanced practitioners. C and C++ programmers interested in different approaches to concurrency and Linux systems programming.

What You Will Learn

  • Explore the Go language from the standpoint of a developer conversant with Unix, Linux, and so on
  • Understand Goroutines, the lightweight threads used for systems and concurrent applications
  • Learn how to translate Unix and Linux systems code in C to Golang code
  • How to write fast and lightweight server code
  • Dive into concurrency with Go
  • Write low-level networking code

In Detail

Go is the new systems programming language for Linux and Unix systems. It is also the language in which some of the most prominent cloud-level systems have been written, such as Docker. Where C programmers used to rule, Go programmers are in demand to write highly optimized systems programming code.

Created by some of the original designers of C and Unix, Go expands the systems programmers toolkit and adds a mature, clear programming language. Traditional system applications become easier to write since pointers are not relevant and garbage collection has taken away the most problematic area for low-level systems code: memory management.

This book opens up the world of high-performance Unix system applications to the beginning Go programmer. It does not get stuck on single systems or even system types, but tries to expand the original teachings from Unix system level programming to all types of servers, the cloud, and the web.

Style and approach

This is the first book to introduce Linux and Unix systems programming in Go, a field for which Go has actually been developed in the first place.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Getting Started with Go and Unix Systems Programming
    1. The structure of the book
    2. What is systems programming?
      1. Learning systems programming
    3. About Go
      1. Getting ready for Go
    4. Two useful Go tools
      1. Advantages and disadvantages of Go
    5. The various states of a Unix process
    6. Exercises
    7. Summary
  3. Writing Programs in Go
    1. Compiling Go code
      1. Checking the size of the executable file
    2. Go environment variables
    3. Using command-line arguments
      1. Finding the sum of the command-line arguments
    4. User input and output
      1. Getting user input
      2. Printing output
    5. Go functions
      1. Naming the return values of a Go function
      2. Anonymous functions
      3. Illustrating Go functions
      4. The defer keyword
      5. Using pointer variables in functions
    6. Go data structures
      1. Arrays
      2. Slices
      3. Maps
        1. Converting an array into a map
      4. Structures
    7. Interfaces
    8. Creating random numbers
    9. Exercises
    10. Summary
  4. Advanced Go Features
    1. Error handling in Go
      1. Functions can return error variables
      2. About error logging
      3. The addCLA.go program revisited
    2. Pattern matching and regular expressions
      1. Printing all the values from a given column of a line
      2. Creating summaries
      3. Finding the number of occurrences
      4. Find and replace
    3. Reflection
      1. Calling C code from Go
      2. Unsafe code
    4. Comparing Go to other programming languages
    5. Analysing software
      1. Using the strace(1) command-line utility
      2. The DTrace utility
        1. Disabling System Integrity Protection on macOS
    6. Unreachable code
    7. Avoiding common Go mistakes
    8. Exercises
    9. Summary
  5. Go Packages, Algorithms, and Data Structures
    1. About algorithms
      1. The Big O notation
    2. Sorting algorithms
      1. The sort.Slice() function
    3. Linked lists in Go
    4. Trees in Go
    5. Developing a hash table in Go
    6. About Go packages
      1. Using standard Go packages
      2. Creating your own packages
        1. Private variables and functions
        2. The init() function
      3. Using your own Go packages
      4. Using external Go packages
        1. The go clean command
    7. Garbage collection
    8. Your environment
    9. Go gets updated frequently!
    10. Exercises
    11. Summary
  6. Files and Directories
    1. Useful Go packages
    2. Command-line arguments revisited!
      1. The flag package
    3. Dealing with directories
      1. About symbolic links
      2. Implementing the pwd(1) command
      3. Developing the which(1) utility in Go
        1. Printing the permission bits of a file or directory
    4. Dealing with files in Go
      1. Deleting a file
      2. Renaming and moving files
    5. Developing find(1) in Go
      1. Traversing a directory tree
        1. Visiting directories only!
    6. The first version of find(1)
      1. Adding some command-line options
      2. Excluding filenames from the find output
      3. Excluding a file extension from the find output
    7. Using regular expressions
      1. Creating a copy of a directory structure
    8. Exercises
    9. Summary
  7. File Input and Output
    1. About file input and output
      1. Byte slices
      2. About binary files
    2. Useful I/O packages in Go
      1. The io package
      2. The bufio package
    3. File I/O operations
      1. Writing to files using fmt.Fprintf()
        1. About io.Writer and io.Reader
      2. Finding out the third column of a line
    4. Copying files in Go
      1. There is more than one way to copy a file!
      2. Copying text files
      3. Using io.Copy
      4. Reading a file all at once!
      5. An even better file copy program
      6. Benchmarking file copying operations
    5. Developing wc(1) in Go
      1. Counting words
      2. The wc.go code!
        1. Comparing the performance of wc.go and wc(1)
      3. Reading a text file character by character
        1. Doing some file editing!
    6. Interprocess communication
    7. Sparse files in Go
    8. Reading and writing data records
    9. File locking in Go
    10. A simplified Go version of the dd utility
    11. Exercises
    12. Summary
  8. Working with System Files
    1. Which files are considered system files?
    2. Logging in Go
    3. Putting data at the end of a file
      1. Altering existing data
    4. About log files
      1. About logging
      2. Logging facilities
      3. Logging levels
      4. The syslog Go package
      5. Processing log files
      6. File permissions revisited
        1. Changing file permissions
        2. Finding other kinds of information about files
    5. More pattern matching examples
      1. A simple pattern matching example
      2. An advanced example of pattern matching
      3. Renaming multiple files using regular expressions
    6. Searching files revisited
      1. Finding the user ID of a user
      2. Finding all the groups a user belongs to
      3. Finding files that belong or do not belong to a given user
      4. Finding files based on their permissions
    7. Date and time operations
      1. Playing with dates and times
      2. Reformatting the times in a log file
    8. Rotating log files
    9. Creating good random passwords
    10. Another Go update
    11. Exercises
    12. Summary
  9. Processes and Signals
    1. About Unix processes and signals
    2. Process management
      1. About Unix signals
    3. Unix signals in Go
    4. The kill(1) command
      1. A simple signal handler in Go
      2. Handling three different signals!
      3. Catching every signal that can be handled
      4. Rotating log files revisited!
    5. Improving file copying
    6. Plotting data
    7. Unix pipes in Go
      1. Reading from standard input
      2. Sending data to standard output
      3. Implementing cat(1) in Go
      4. The plotIP.go utility revisited
    8. Unix sockets in Go
    9. RPC in Go
    10. Programming a Unix shell in Go
    11. Yet another minor Go update
    12. Exercises
    13. Summary
  10. Goroutines - Basic Features
    1. About goroutines
      1. Concurrency and parallelism
    2. The sync Go packages
      1. A simple example
        1. Creating multiple goroutines
      2. Waiting for goroutines to finish their jobs
        1. Creating a dynamic number of goroutines
      3. About channels
        1. Writing to a channel
        2. Reading from a channel
        3. Explaining h1s.go
    3. Pipelines
    4. A better version of wc.go
      1. Calculating totals
      2. Doing some benchmarking
    5. Exercises
    6. Summary
  11. Goroutines - Advanced Features
    1. The Go scheduler
    2. The sync Go package
    3. The select keyword
    4. Signal channels
    5. Buffered channels
    6. About timeouts
      1. An alternative way to implement timeouts
    7. Channels of channels
    8. Nil channels
    9. Shared memory
      1. Using sync.Mutex
      2. Using sync.RWMutex
    10. The dWC.go utility revisited
      1. Using a buffered channel
      2. Using shared memory
      3. More benchmarking
    11. Detecting race conditions
    12. About GOMAXPROCS
    13. Exercises
    14. Summary
  12. Writing Web Applications in Go
    1. What is a web application?
    2. About the net/http Go package
    3. Developing web clients
      1. Fetching a single URL
        1. Setting a timeout
      2. Developing better web clients
    4. A small web server
      1. The http.ServeMux type
        1. Using http.ServeMux
    5. The html/template package
    6. About JSON
      1. Saving JSON data
      2. Parsing JSON data
      3. Using Marshal() and Unmarshal()
    7. Using MongoDB
      1. Basic MongoDB administration
      2. Using the MongoDB Go driver
      3. Creating a Go application that displays MongoDB data
      4. Creating an application that displays MySQL data
    8. A handy command-line utility
    9. Exercises
    10. Summary
  13. Network Programming
    1. About network programming
      1. About TCP/IP
      2. About TCP
        1. The TCP handshake!
      3. About UDP and IP
      4. About Wireshark and tshark
      5. About the netcat utility
    2. The net Go standard package
    3. Unix sockets revisited
      1. A Unix socket server
      2. A Unix socket client
    4. Performing DNS lookups
      1. Using an IP address as input
      2. Using a host name as input
      3. Getting NS records for a domain
    5. Developing a simple TCP server
    6. Developing a simple TCP client
      1. Using other functions for the TCP server
      2. Using alternative functions for the TCP client
    7. Developing a simple UDP server
    8. Developing a simple UDP client
    9. A concurrent TCP server
    10. Remote procedure call (RPC)
      1. An RPC server
      2. An RPC client
    11. Exercises
    12. Summary

Product information

  • Title: Go Systems Programming
  • Author(s): Mihalis Tsoukalos
  • Release date: September 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787125643