An Introduction to Programming in Go (Caleb Doxsey) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Caleb Doxsey

GO

This book is a short, concise introduction to computer programming using the language Go. Designed by Google, Go is a general purpose programming language with modern features, clean syntax and a robust well-documented common library, making it an ideal language to learn as your first programming language. Contents 1 Getting Started 1 1.1 Files and Folders 2 1.2 The Terminal 5 1.3 Text Editors 9 1.4 Go Tools 13 2 Your First Program 15 2.1 How to Read a Go Program 17 3 Types 23 3.1 Numbers 24 3.2 Strings 29 3.3 Booleans 31 4 Variables 35 4.1 How to Name a Variable 39 4.2 Scope 40 4.3 Constants 43 4.4 Defining Multiple Variables 44 4.5 An Example Program 45 5 Control Structures 47 5.1 For 48 5.2 If 51 5.3 Switch 55 6 Arrays, Slices and Maps 58 6.1 Arrays 58 6.2 Slices 64 6.3 Maps 67 7 Functions 76 7.1 Your Second Function 77 7.2 Returning Multiple Values 82 7.3 Variadic Functions 82 7.4 Closure 84 7.5 Recursion 86 7.6 Defer, Panic & Recover 88 8 Pointers 92 8.1 The * and & operators 93 8.2 new 94 9 Structs and Interfaces 97 9.1 Structs 98 9.2 Methods 101 9.3 Interfaces 104 10 Concurrency 108 10.1 Goroutines 108 10.2 Channels 111 11 Packages 120 11.1 Creating Packages 121 11.2 Documentation 124 12 Testing 127 13 The Core Packages 132 13.1 Strings 132 13.2 Input / Output 134 13.3 Files & Folders 135 13.4 Errors 140 13.5 Containers & Sort 141 13.6 Hashes & Cryptography 144 13.7 Servers 147 13.8 Parsing Command Line Arguments 155 13.9 Synchronization Primitives 156 14 Next Steps 159 14.1 Study the Masters 159 14.2 Make Something 160 14.3 Team Up 161

📄 File Format: PDF
💾 File Size: 11.6 MB
6
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
An Introduction to Programming in Go Copyright © 2012 by Caleb Doxsey All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system without the written permission of the author, except where permitted by law. ISBN: 978-1478355823 Cover art by Abigail Doxsey Anderson. Portions of this text are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.
📄 Page 3
Contents 1 Getting Started 1 1.1 Files and Folders 2 1.2 The Terminal 5 1.3 Text Editors 9 1.4 Go Tools 13 2 Your First Program 15 2.1 How to Read a Go Program 17 3 Types 23 3.1 Numbers 24 3.2 Strings 29 3.3 Booleans 31 4 Variables 35 4.1 How to Name a Variable 39 4.2 Scope 40 4.3 Constants 43 4.4 Defining Multiple Variables 44 4.5 An Example Program 45 5 Control Structures 47 5.1 For 48 5.2 If 51 5.3 Switch 55 6 Arrays, Slices and Maps 58 6.1 Arrays 58 6.2 Slices 64 6.3 Maps 67 7 Functions 76 7.1 Your Second Function 77 7.2 Returning Multiple Values 82 7.3 Variadic Functions 82
📄 Page 4
7.4 Closure 84 7.5 Recursion 86 7.6 Defer, Panic & Recover 88 8 Pointers 92 8.1 The * and & operators 93 8.2 new 94 9 Structs and Interfaces 97 9.1 Structs 98 9.2 Methods 101 9.3 Interfaces 104 10 Concurrency 108 10.1 Goroutines 108 10.2 Channels 111 11 Packages 120 11.1 Creating Packages 121 11.2 Documentation 124 12 Testing 127 13 The Core Packages 132 13.1 Strings 132 13.2 Input / Output 134 13.3 Files & Folders 135 13.4 Errors 140 13.5 Containers & Sort 141 13.6 Hashes & Cryptography 144 13.7 Servers 147 13.8 Parsing Command Line Arguments 155 13.9 Synchronization Primitives 156 14 Next Steps 159 14.1 Study the Masters 159 14.2 Make Something 160 14.3 Team Up 161
📄 Page 5
1 Getting Started Computer programming is the art, craft and science of writing programs which define how computers operate. This book will teach you how to write computer pro- grams using a programming language designed by Google named Go. Go is a general purpose programming language with advanced features and a clean syntax. Because of its wide availability on a variety of platforms, its robust well-documented common library, and its focus on good software engineering principles, Go is an ideal language to learn as your first programming language. The process we use to write software using Go (and most programming languages) is fairly straightfor- ward: 1. Gather requirements 2. Find a solution 3. Write source code to implement the solution 1
📄 Page 6
Getting Started 2 4. Compile the source code into an executable 5. Run and test the program to make sure it works This process is iterative (meaning its done many times) and the steps usually overlap. But before we write our first program in Go there are a few prerequi- site concepts we need to understand. 1.1 Files and Folders A file is a collection of data stored as a unit with a name. Modern operating systems (like Windows or Mac OSX) contain millions of files which store a large variety of different types of information – everything from text documents to executable programs to multi- media files. All files are stored in the same way on a computer: they all have a name, a definite size (measured in bytes) and an associated type. Typically the file's type is signified by the file's extension – the part of the file name that comes after the last .. For example a file with the name hello.txt has the extension txt which is used to represent textual data. Folders (also called directories) are used to group files together. They can also contain other folders. On Win-
📄 Page 7
3 Getting Started dows file and folder paths (locations) are represented with the \ (backslash) character, for example: C:\Users\john\example.txt. example.txt is the file name, it is contained in the folder john, which is itself contained in the folder Users which is stored on drive C (which represents the primary physical hard drive in Windows). On OSX (and most other operating sys- tems) file and folder paths are represented with the / (forward slash) character, for example: /Users/john/example.txt. Like on Windows example.txt is the file name, it is contained in the folder john, which is in the folder Users. Unlike Win- dows, OSX does not specify a drive letter where the file is stored.
📄 Page 8
Getting Started 4 Windows On Windows files and folders can be browsed using Windows Explorer (accessible by double-clicking “My Computer” or typing win+e):
📄 Page 9
5 Getting Started OSX On OSX files and folders can be browsed using Finder (accessible by clicking the Finder icon – the face icon in the lower left bar): 1.2 The Terminal Most of the interactions we have with computers today are through sophisticated graphical user interfaces (GUIs). We use keyboards, mice and touchscreens to interact with visual buttons or other types of controls that are displayed on a screen. It wasn't always this way. Before the GUI we had the terminal – a simpler textual interface to the computer
📄 Page 10
Getting Started 6 where rather than manipulating buttons on a screen we issued commands and received replies. We had a conversation with the computer. And although it might appear that most of the comput- ing world has left behind the terminal as a relic of the past, the truth is that the terminal is still the funda- mental user interface used by most programming lan- guages on most computers. The Go programming lan- guage is no different, and so before we write a program in Go we need to have a rudimentary understanding of how a terminal works. Windows In Windows the terminal (also known as the command line) can be brought up by typing the windows key + r (hold down the windows key then press r), typing cmd.exe and hitting enter. You should see a black win- dow appear that looks like this:
📄 Page 11
7 Getting Started By default the command line starts in your home di- rectory. (In my case this is C:\Users\caleb) You issue commands by typing them in and hitting enter. Try entering the command dir, which lists the contents of a directory. You should see something like this: C:\Users\caleb>dir Volume in drive C has no label. Volume Serial Number is B2F5-F125 Followed by a list of the files and folders contained in your home directory. You can change directories by us- ing the command cd. For example you probably have a folder called Desktop. You can see its contents by en- tering cd Desktop and then entering dir. To go back to your home directory you can use the special directory name .. (two periods next to each other): cd ... A sin- gle period represents the current folder (known as the working folder), so cd . doesn't do anything. There are
📄 Page 12
Getting Started 8 a lot more commands you can use, but this should be enough to get you started. OSX In OSX the terminal can be reached by going to Finder → Applications → Utilities → Terminal. You should see a window like this: By default the terminal starts in your home directory. (In my case this is /Users/caleb) You issue commands by typing them in and hitting enter. Try entering the command ls, which lists the contents of a directory. You should see something like this:
📄 Page 13
9 Getting Started caleb-min:~ caleb$ ls Desktop Downloads Movies Pictures Documents Library Music Public These are the files and folders contained in your home directory (in this case there are no files). You can change directories using the cd command. For example you probably have a folder called Desktop. You can see its contents by entering cd Desktop and then entering ls. To go back to your home directory you can use the special directory name .. (two periods next to each other): cd ... A single period represents the current folder (known as the working folder), so cd . doesn't do anything. There are a lot more commands you can use, but this should be enough to get you started. 1.3 Text Editors The primary tool programmers use to write software is a text editor. Text editors are similar to word process- ing programs (Microsoft Word, Open Office, …) but un- like such programs they don't do any formatting, (No bold, italic, …) instead they operate only on plain text. Both OSX and Windows come with text editors but they are highly limited and I recommend installing a better one. To make the installation of this software easier an in-
📄 Page 14
Getting Started 10 staller is available at the book's website: http://www.golang-book.com/. This installer will in- stall the Go tool suite, setup environmental variables and install a text editor. Windows For windows the installer will install the Scite text ed- itor. You can open it by going to Start → All Programs → Go → Scite. You should see something like this: The text editor contains a large white text area where text can be entered. To the left of this text area you can see the line numbers. At the bottom of the window
📄 Page 15
11 Getting Started is a status bar which displays information about the file and your current location in it (right now it says that we are on line 1, column 1, text is being inserted normally, and we are using windows-style newlines). You can open files by going to File → Open and brows- ing to your desired file. Files can be saved by going to File → Save or File → Save As. As you work in a text editor it is useful to learn key- board shortcuts. The menus list the shortcuts to their right. Here are a few of the most common: • Ctrl + S – save the current file • Ctrl + X – cut the currently selected text (remove it and put it in your clipboard so it can be pasted later) • Ctrl + C – copy the currently selected text • Ctrl + V – paste the text currently in the clipboard • Use the arrow keys to navigate, Home to go to the beginning of the line and End to go to the end of the line • Hold down shift while using the arrow keys (or Home and End) to select text without using the mouse • Ctrl + F – brings up a find in file dialog that you can use to search the contents of a file
📄 Page 16
Getting Started 12 OSX For OSX the installer installs the Text Wrangler text editor: Like Scite on Windows Text Wrangler contains a large white area where text is entered. Files can be opened by going to File → Open. Files can be saved by going to File → Save or File → Save As. Here are some useful keyboard shortcuts: (Command is the ⌘ key)
📄 Page 17
13 Getting Started • Command + S – save the current file • Command + X – cut the currently selected text (re- move it and put it in your clipboard so it can be pasted later) • Command + C – copy the currently selected text • Command + V – paste the text currently in the clip- board • Use the arrow keys to navigate • Command + F – brings up a find in file dialog that you can use to search the contents of a file 1.4 Go Tools Go is a compiled programming language, which means source code (the code you write) is translated into a language that your computer can understand. There- fore before we can write a Go program, we need the Go compiler. The installer will setup Go for you automatically. We will be using version 1 of the language. (More informa- tion can be found at http://www.golang.org) Let's make sure everything is working. Open up a ter- minal and type the following: go version
📄 Page 18
Getting Started 14 You should see the following: go version go1.0.2 Your version number may be slightly different. If you get an error about the command not being recognized try restarting your computer. The Go tool suite is made up of several different com- mands and sub-commands. A list of those commands is available by typing: go help We will see how they are used in subsequent chapters.
📄 Page 19
2 Your First Program Traditionally the first program you write in any pro- gramming language is called a “Hello World” program – a program that simply outputs Hello World to your terminal. Let's write one using Go. First create a new folder where we can store our pro- gram. The installer you used in chapter 1 created a folder in your home directory named Go. Create a folder named ~/Go/src/golang-book/chapter2. (Where ~ means your home directory) From the terminal you can do this by entering the following commands: mkdir Go/src/golang-book mkdir Go/src/golang-book/chapter2 Using your text editor type in the following: 15
📄 Page 20
Your First Program 16 package main import "fmt" // this is a comment func main() { fmt.Println("Hello World") } Make sure your file is identical to what is shown here and save it as main.go in the folder we just created. Open up a new terminal and type in the following: cd Go/src/golang-book/chapter2 go run main.go You should see Hello World displayed in your termi- nal. The go run command takes the subsequent files (separated by spaces), compiles them into an exe- cutable saved in a temporary directory and then runs the program. If you didn't see Hello World displayed you may have made a mistake when typing in the pro- gram. The Go compiler will give you hints about where the mistake lies. Like most compilers, the Go compiler is extremely pedantic and has no tolerance for mis- takes.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List