📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
(This page has no text content)
📄 Page
4
Copyright Go Programming by Example Agus Kurniawan 1st Edition, 2015 Copyright © 2015 Agus Kurniawan * Cover photo is credit to Fajar Ramadhany, Bataviasoft, http://bataviasoft.com/. ** Go logo is taken from https://blog.golang.org/gopher .
📄 Page
5
Table of Contents Copyright Preface 1. Development Environment 1.1 Installation 1.2 Development Tools 1.3 Hello World 1.4 Go Packages 2. Go Programming Language 2.1 Common Rule 2.2 Variables 2.2.1 Declaring Variable 2.2.2 Assigning Variables 2.2.3 Demo 2.3 Comment 2.4 Arithmetic Operations 2.5 Mathematical Functions 2.6 Increment and Decrement 2.7 Getting Input from Keyboard 2.8 Comparison Operators 2.9 Logical Operators 2.10 Decision 2.10.1 if..then 2.10.2 switch..case 2.11 Iteration - for 2.12 Iteration - while 2.13 break and continue 3. Arrays, Slices and Maps 3.1 Array 3.2 Slice
📄 Page
6
3.3 Map 4. Functions 4.1 Creating A Simple Function 4.2 Function with Parameters 4.3 Function with Returning Value 4.4 Function with Multiple Returning Values 4.5 Function with Multiple Parameters and Returning Value 4.6 Closure Function 4.7 Recursion Function 4.8 Testing 5. Pointers 5.1 Pointer in Go 5.2 Demo: Singly Linked List 6. Structs and Methods 6.1 Structs 6.2 Methods 7. String Operations 7.1 Getting Started 7.2 Concatenating Strings 7.3 String To Numeric 7.4 Numeric to String 7.5 String Parser 7.6 Check String Data Length 7.7 Copy Data 7.8 Upper and Lower Case Characters 7.9 Testing A Program 8. File Operations 8.1 Getting Started 8.2 Writing Data Into A File 8.3 Reading Data From A File 8.4 Writing All
📄 Page
7
9. Error Handling and Logging 9.1 Error Handling 9.2 defer, panic(), and recover() 9.3 try..catch 9.4 Logging 10. Building Own Go Package 10.1 Creating Simple Module 10.2 Building Own Package 11. Concurrency 11.1 Getting Started 11.2 Goroutines 11.3 Synchronizing Goroutines 11.4 Channels 12. Encoding 12.1 Getting Started 12.2 Encoding Base64 12.3 Hexadecimal 12.4 JSON 12.5 XML 12.6 CSV 13. Hashing and Cryptography 13.1 Getting Started 13.2 Hashing 13.2.1 Hashing with MD5 13.2.2 Hashing with SHA256 13.2.3 Hashing with Key Using HMAC 13.2.4 Testing 13.3 Cryptography 13.3.1 Symmetric Cryptography 13.3.2 Asymmetric Cryptography 14. Database Programming
📄 Page
8
14.1 Database for Go 14.2 MySQL Driver for Go 14.3 Testing Connection 14.4 Querying 15. Socket Programming 15.1 Socket Module 15.2 Hello World 15.3 Client/Server Socket 15.3.1 Server Socket 15.3.2 Client Socket 15.3.3 Testing Source Code Contact
📄 Page
9
Preface Go was created by Robert Griesemer, Rob Pike, and Ken Thompson at Google in 2007. This book is a reference to the Go programming language. It describes all the elements of the language and illustrates their use with code examples. Agus Kurniawan Berlin & Depok, February 2015
📄 Page
10
1. Development Environment This chapter explains introduction of Go. The official web of Go could be found on https://golang.org/. What is Go? Based on information from website, we could know what it is. Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
📄 Page
11
1.1 Installation Installation of Go application is easy. For Windows and Mac Platform, you download setup file from Go website, http://golang.org/doc/install. Run it and follow installation commands. The next step is to configure GOROOT path. For Windows platform, you can add GOROOT variable on Environment Variables. For Mac/Linux, you can it on your bash profile.
📄 Page
12
For Windows platform, you can add GO installation path, for instance my Go installation path is c:/go/bin, into PATH variable on Environment Variables.
📄 Page
13
After configured, you can verify Go version by typing this command on your Terminal or CMD for Windows. $ go version A sample output can be seen in Figure below, Mac platform.
📄 Page
14
The output of program on Windows platform.
📄 Page
15
1.2 Development Tools Basically, you can use any text editor to write Go code. The following is a list of text editor: vim nano Intellij IDEA, https://www.jetbrains.com/idea/ Sublime text, http://www.sublimetext.com/ A sample screenshot for Intellij IDEA is shown in Figure below.
📄 Page
16
1.3 Hello World How to get started with Go? well, it is easy. Firstly, create a folder, called hello. $ mkdir hello $ cd hello Then, create a file, called main.go, on the hello folder. Write this code for main.go file. package main import "fmt" func main() { fmt.Println("Hello, go!") } Save this file. Then, back to your Terminal. You can build and run it. $ go build $ go run main.go In this case, it will generate an executable file based on your platform. A sample program output can be seen in Figure below.
📄 Page
17
1.4 Go Packages There are a lot of Go packages that you can use. The list of Go packages can seen on this link https://golang.org/pkg/. In this book, we will explore some Go standard packages. We also try to build own Go package.
📄 Page
18
2. Go Programming Language This chapter explains the basic of Go programming language.
📄 Page
19
2.1 Common Rule Go language uses “C family” as primary language but we don’t write “;” at the end of syntax. Here is the syntax rule: syntax_code1 syntax_code2 syntax_code3
📄 Page
20
2.2 Variables In this section, we explore how to define a variable and assign its value. 2.2.1 Declaring Variable To declare a variable called myvar and data type is data_type1, you can write the following script: var myvar data_type1 The following is a sample script to declare some variables. var str string var n, m int var mn float32 We also can define multiple variables as follows. var ( name string email string age int ) Once declared, these variables can be used to store any type data. 2.2.2 Assigning Variables Variable that you already declare can be assigned by a value. It can done using the equals sign (=). For example, variable str will assign a string value “Hello World”, you would write this: str = "Hello World" n = 10 m = 50 mn = 2.45 We also can declare a variable and assign its value. var city string = "London" var x int = 100