Learning Go - Miek

Learning Go

Author: Miek Gieben

Thanks to: Go Authors, Google

With the help and contributions from:

(in alphabetical order)

Adam J. Gray, Alex Sychev, Alexey Chernenkov, Andrea Spadaccini, Andrey Mirtchovski, Anthony Magro, Babu Sreekanth, Ben Bullock, Bob Cunningham, Brian Fallik, Cecil New,

Damian Gryski, Dan Kortschak, David Otton, Fabian Becker, Filip Zaludek, Hadi Amiri, Haiping Fan, Jaap Akkerhuis, JC van Winkel, Jeroen Bulten, Jinpu Hu, John Shahid, Jonathan Kans, Joshua Stein, Makoto Inoue, Mayuresh Kathe, "mem", Michael Stapelberg, Olexandr Shalakhin, Paulo Pinto, Peter Kleiweg, Philipp Schmidt, Robert Johnson, Russel Winder, Sonia Keys, Stefan Schroeder, Thomas Kapplet, T.J. Yang, "Cobold", "Simoc", "Uriel", Xing Xing.

And with minor contributions from:

Alexander Katasonov, Daniele Pala, Iaroslav Tymchenko, Nicolas Kaiser, Marco Ynema.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

Miek Gieben ? ?2010 - 2012

This work is licensed under the Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. All example code used in this book is hereby put in the public domain. "Learning Go" has been translated into:

? Chinese, by Xing Xing, :

Learning as we Go (1.0)

Supports the Go 1.1 release

Contents

1 Introduction

2

Official documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Compiling and running code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Settings used in this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Variables, types and keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Operators and built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Go keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Arrays, slices and maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2 Functions

24

Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Multiple return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Named result parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Deferred code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Variadic parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Panic and recovering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3 Packages

40

Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Documenting packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Testing packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Useful packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

4 Beyond the basics

50

Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Defining your own types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

5 Interfaces

66

Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Interface names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

A sorting example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

6 Concurrency

78

More on channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

7 Communication

86

io.Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

ii Chapter: Contents

Some examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Command line arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Executing commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

A Colophon

100

Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

License and copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

B Index

102

C Bibliography

104

List of Exercises

1 (1) Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 (0) For-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3 (0) FizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4 (1) Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5 (1) Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 6 (0) Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 7 (0) Integer ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 8 (1) Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 9 (1) Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 10 (1) Var args . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 11 (1) Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 12 (1) Map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 13 (0) Minimum and maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 14 (1) Bubble sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 15 (1) Functions that return functions . . . . . . . . . . . . . . . . . . . . . . . . . . 32 16 (0) Stack as package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 17 (2) Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 18 (1) Pointer arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 19 (2) Map function with interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 20 (1) Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 21 (1) Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 22 (1) Cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 23 (2) Method calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 24 (1) Interfaces and compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 25 (1) Pointers and reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 26 (2) Interfaces and max() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 27 (1) Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 28 (2) Fibonacci II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 29 (2) Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 30 (0) Word and letter count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 31 (0) Uniq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 32 (2) Quine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 33 (1) Echo server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 34 (2) Number cruncher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 35 (1) Finger daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download