Go Systems Programming: Master Linux and Unix system level programming with Go

Go Systems Programming: Master Linux and Unix system level programming with Go

Go Systems Programming: Master Linux and Unix system level programming with Go
Автор: Tsoukalos Mihalis
Дата выхода: 2017
Издательство: Packt Publishing Limited
Количество страниц: 459
Размер файла: 2.4 MB
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы  Дополнительные материалы 

 Cover....1

Title Page....2

Copyright....3

Credits....4

About the Author....5

About the Reviewer....6

www.PacktPub.com....7

Customer Feedback....8

Table of Contents....9

Preface....17

Chapter 1: Getting Started with Go and Unix Systems Programming....25

The structure of the book....26

What is systems programming?....26

Learning systems programming....28

About Go....29

Getting ready for Go....30

Two useful Go tools....31

Advantages and disadvantages of Go....35

The various states of a Unix process....36

Exercises....38

Summary....38

Chapter 2: Writing Programs in Go....39

Compiling Go code....39

Checking the size of the executable file....42

Go environment variables....43

Using command-line arguments....45

Finding the sum of the command-line arguments....46

User input and output....49

Getting user input....49

Printing output....51

Go functions....51

Naming the return values of a Go function....52

Anonymous functions....52

Illustrating Go functions....52

The defer keyword....55

Using pointer variables in functions....58

Go data structures....59

Arrays....59

Slices....61

Maps....64

Converting an array into a map....66

Structures....67

Interfaces....69

Creating random numbers....72

Exercises....74

Summary....75

Chapter 3: Advanced Go Features....76

Error handling in Go....76

Functions can return error variables....77

About error logging....79

The addCLA.go program revisited....81

Pattern matching and regular expressions....82

Printing all the values from a given column of a line....84

Creating summaries....85

Finding the number of occurrences....87

Find and replace....89

Reflection....90

Calling C code from Go....92

Unsafe code....93

Comparing Go to other programming languages....94

Analysing software....96

Using the strace(1) command-line utility....96

The DTrace utility....99

Disabling System Integrity Protection on macOS....102

Unreachable code....102

Avoiding common Go mistakes....104

Exercises....104

Summary....105

Chapter 4: Go Packages, Algorithms, and Data Structures....106

About algorithms....107

The Big O notation....107

Sorting algorithms....108

The sort.Slice() function....108

Linked lists in Go....110

Trees in Go....114

Developing a hash table in Go....116

About Go packages....120

Using standard Go packages....120

Creating your own packages....121

Private variables and functions....123

The init() function....123

Using your own Go packages....124

Using external Go packages....125

The go clean command....128

Garbage collection....128

Your environment....130

Go gets updated frequently!....132

Exercises....132

Summary....133

Chapter 5: Files and Directories....134

Useful Go packages....135

Command-line arguments revisited!....135

The flag package....135

Dealing with directories....138

About symbolic links....139

Implementing the pwd(1) command....140

Developing the which(1) utility in Go....142

Printing the permission bits of a file or directory....145

Dealing with files in Go....146

Deleting a file....146

Renaming and moving files....148

Developing find(1) in Go....150

Traversing a directory tree....151

Visiting directories only!....152

The first version of find(1)....153

Adding some command-line options....155

Excluding filenames from the find output....158

Excluding a file extension from the find output....160

Using regular expressions....161

Creating a copy of a directory structure....162

Exercises....167

Summary....167

Chapter 6: File Input and Output....168

About file input and output....169

Byte slices....169

About binary files....171

Useful I/O packages in Go....172

The io package....173

The bufio package....174

File I/O operations....176

Writing to files using fmt.Fprintf()....176

About io.Writer and io.Reader....177

Finding out the third column of a line....179

Copying files in Go....182

There is more than one way to copy a file!....182

Copying text files....182

Using io.Copy....182

Reading a file all at once!....184

An even better file copy program....186

Benchmarking file copying operations....189

Developing wc(1) in Go....194

Counting words....194

The wc.go code!....194

Comparing the performance of wc.go and wc(1)....199

Reading a text file character by character....200

Doing some file editing!....202

Interprocess communication....204

Sparse files in Go....204

Reading and writing data records....207

File locking in Go....210

A simplified Go version of the dd utility....213

Exercises....216

Summary....216

Chapter 7: Working with System Files....217

Which files are considered system files?....218

Logging in Go....218

Putting data at the end of a file....218

Altering existing data....220

About log files....222

About logging....223

Logging facilities....223

Logging levels....223

The syslog Go package....224

Processing log files....226

File permissions revisited....230

Changing file permissions....232

Finding other kinds of information about files....234

More pattern matching examples....237

A simple pattern matching example....237

An advanced example of pattern matching....240

Renaming multiple files using regular expressions....243

Searching files revisited....245

Finding the user ID of a user....245

Finding all the groups a user belongs to....247

Finding files that belong or do not belong to a given user....249

Finding files based on their permissions....252

Date and time operations....254

Playing with dates and times....254

Reformatting the times in a log file....256

Rotating log files....258

Creating good random passwords....261

Another Go update....263

Exercises....263

Summary....264

Chapter 8: Processes and Signals....265

About Unix processes and signals....266

Process management....266

About Unix signals....267

Unix signals in Go....268

The kill(1) command....268

A simple signal handler in Go....269

Handling three different signals!....271

Catching every signal that can be handled....273

Rotating log files revisited!....275

Improving file copying....278

Plotting data....282

Unix pipes in Go....289

Reading from standard input....290

Sending data to standard output....292

Implementing cat(1) in Go....293

The plotIP.go utility revisited....294

Unix sockets in Go....302

RPC in Go....303

Programming a Unix shell in Go....304

Yet another minor Go update....306

Exercises....306

Summary....307

Chapter 9: Goroutines — Basic Features....308

About goroutines....309

Concurrency and parallelism....309

The sync Go packages....310

A simple example....310

Creating multiple goroutines....311

Waiting for goroutines to finish their jobs....313

Creating a dynamic number of goroutines....315

About channels....317

Writing to a channel....317

Reading from a channel....319

Explaining h1s.go....320

Pipelines....321

A better version of wc.go....323

Calculating totals....326

Doing some benchmarking....330

Exercises....332

Summary....332

Chapter 10: Goroutines — Advanced Features....333

The Go scheduler....334

The sync Go package....334

The select keyword....334

Signal channels....337

Buffered channels....340

About timeouts....342

An alternative way to implement timeouts....344

Channels of channels....346

Nil channels....348

Shared memory....349

Using sync.Mutex....351

Using sync.RWMutex....355

The dWC.go utility revisited....358

Using a buffered channel....358

Using shared memory....361

More benchmarking....364

Detecting race conditions....365

About GOMAXPROCS....368

Exercises....370

Summary....370

Chapter 11: Writing Web Applications in Go....371

What is a web application?....372

About the net/http Go package....372

Developing web clients....372

Fetching a single URL....372

Setting a timeout....374

Developing better web clients....376

A small web server....379

The http.ServeMux type....382

Using http.ServeMux....382

The html/template package....386

About JSON....391

Saving JSON data....392

Parsing JSON data....394

Using Marshal() and Unmarshal()....396

Using MongoDB....398

Basic MongoDB administration....398

Using the MongoDB Go driver....401

Creating a Go application that displays MongoDB data....404

Creating an application that displays MySQL data....408

A handy command-line utility....411

Exercises....415

Summary....416

Chapter 12: Network Programming....417

About network programming....418

About TCP/IP....418

About TCP....418

The TCP handshake!....419

About UDP and IP....419

About Wireshark and tshark....420

About the netcat utility....422

The net Go standard package....422

Unix sockets revisited....423

A Unix socket server....423

A Unix socket client....425

Performing DNS lookups....427

Using an IP address as input....428

Using a host name as input....429

Getting NS records for a domain....431

Developing a simple TCP server....432

Developing a simple TCP client....435

Using other functions for the TCP server....437

Using alternative functions for the TCP client....439

Developing a simple UDP server....440

Developing a simple UDP client....442

A concurrent TCP server....444

Remote procedure call (RPC)....446

An RPC server....447

An RPC client....450

Exercises....452

Summary....453

Index....454

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.

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

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.


Похожее:

Список отзывов:

Нет отзывов к книге.