You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: manuscript/0.0installation.md
+4-8
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# Installation
2
+
2
3
If you know about installation or have installed Go, you can skip to [Tools](0.1tools.md).
3
4
4
5
This chapter is taken from [install page](https://golang.org/doc/install) verbatim, except for the changes to be made to adapt to this book's styling format.
5
6
6
-
7
7
## System requirements
8
8
9
9
Go binary distributions are available for these supported operating systems and architectures. Please ensure your system meets these requirements before proceeding. If your OS or architecture is not on the list, you may be able to install from source or use gccgo instead
@@ -21,10 +21,8 @@ Go binary distributions are available for these supported operating systems and
21
21
‡You only need to install the command line tools for Xcode. If you have already installed Xcode 4.3+, you can
22
22
install it from the Components tab of the Downloads preferences panel.
23
23
24
-
25
24
## Install the Go tools
26
25
27
-
28
26
If you are upgrading from an older version of Go you must first remove the existing version.
29
27
Linux, Mac OS X, and FreeBSD tarballs
30
28
@@ -40,7 +38,6 @@ Add` /usr/local/go/bin` to the PATH environment variable. You can do this by add
40
38
41
39
`export PATH=$PATH:/usr/local/go/bin`
42
40
43
-
44
41
### Installing to a custom location
45
42
46
43
The Go binary distributions assume they will be installed in `/usr/local/go` (or `c:\Go` under Windows), but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed.
@@ -69,7 +66,6 @@ Open the MSI file and follow the prompts to install the Go tools. By default, th
69
66
70
67
The installer should put the `c:\Go\bin` directory in your PATH environment variable. You may need to restart any open command prompts for the change to take effect.
71
68
72
-
73
69
#### Zip archive
74
70
75
71
Download the zip file and extract it into the directory of your choice (we suggest c:\Go).
@@ -115,20 +111,20 @@ If you see the "hello, world" message then your Go installation is working.
115
111
116
112
Before rushing off to write Go code please read the How to Write Go Code document, which describes some essential concepts about using the Go tools.
117
113
118
-
###Uninstalling Go
114
+
## Uninstalling Go
119
115
120
116
To remove an existing Go installation from your system delete the go directory. This is usually `/usr/local/go` under Linux, Mac OS X, and FreeBSD or `c:\Go` under Windows.
121
117
122
118
You should also remove the Go bin directory from your PATH environment variable. Under Linux and FreeBSD you should edit `/etc/profile` or `$HOME/.profile`. If you installed Go with the Mac OS X package then you should remove the `/etc/paths.d/go` file. Windows users should read the section about setting environment variables under Windows.
123
119
124
-
###Getting help
120
+
## Getting help
125
121
126
122
For real-time help, ask the helpful gophers in `#go-nuts` on the Freenode IRC server.
127
123
128
124
The official mailing list for discussion of the Go language is Go Nuts.
Copy file name to clipboardexpand all lines: manuscript/0.1tools.md
+7-8
Original file line number
Diff line number
Diff line change
@@ -64,18 +64,17 @@ We can build our application using `go build`. It parses all the `.go` files exc
64
64
65
65
Example: `go build -o tasks`
66
66
67
-
######Build time
67
+
#### Build time
68
68
69
69
By default, `go build` builds the entire application and the depending libraries, into a static binary and later throws all away. This results in rebuilding everything every single time `go build` is executed. For caching the library builds, use `go install` first and `go build` later.
70
70
71
-
######Cross compilation
71
+
#### Cross compilation
72
72
73
73
Go allows cross compilation. We have to pass the OS name as linux/darwin/windows as GOOS as shown in the below commands.
74
74
75
75
env GOOS=darwin GOARCH=386 go build -o tasks.app
76
76
env GOOS=windows GOARCH=386 go build -o tasks.exe
77
77
78
-
79
78
### go install
80
79
81
80
Creates a statically linked binary and places it in `$GOPATH/bin`.
@@ -91,17 +90,17 @@ In Linux/Unix, this is done using: `export PATH=$PATH:$GOPATH/bin`. This line ne
91
90
92
91
The profile files are present in the home folder. Do a `cd ~` and check for either of the files mentioned above.
93
92
94
-
####go run
93
+
### go run
95
94
96
95
go run combines building and running the application in one command.
97
96
98
97
It generates a binary in the temp folder and executes it. The binary file isn't retained after the run.
99
98
100
-
####go get
99
+
### go get
101
100
102
101
This is the package manager in Go. It internally clones the version control repository parameter passed to it, can be any local/remote git repository. It then runs `go install` on the library, making the library available in `$GOPATH/pkg`. If the repository doesn't have any buildable files then go get might complain, but that happens after the repository is cloned.
103
102
104
-
####go clean
103
+
### go clean
105
104
106
105
This command is for cleaning files that are generated by compilers, including the following files:
107
106
@@ -116,7 +115,7 @@ This command is for cleaning files that are generated by compilers, including th
116
115
DIR.test(.exe) // generated by go test -c
117
116
MAINFILE(.exe) // generated by go build MAINFILE.go
118
117
119
-
####Other commands
118
+
### Other commands
120
119
121
120
Go provides more commands than those we've just talked about.
122
121
@@ -127,7 +126,7 @@ Go provides more commands than those we've just talked about.
127
126
128
127
For details about specific commands, `go help <command>`.
Copy file name to clipboardexpand all lines: manuscript/02.1IntroductionGo.md
+6-4
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ A list of features:
42
42
43
43
Let's start with the customary Hello World.
44
44
45
-
####First Program
45
+
### First Program
46
46
47
47
```golang
48
48
package main
@@ -58,12 +58,14 @@ It prints following information.
58
58
59
59
Hello, world or καλημ ρα κóσμ
60
60
61
-
#### Explanation
61
+
### Explanation
62
+
62
63
We import the format package, `fmt` for short. Write the main function of the main package and run the code. We can access only the `Exported` objects of any library in Go. Exported means that the names should start with a capital letter. The compiler will find the `main` function of the `main` package whenever we build or execute any Go code.
63
64
64
65
We printed non ASCII characters. Go supports UTF-8 by default.
65
66
66
-
#### The main package
67
+
### The main package
68
+
67
69
It is mandatory for each Go program to be a part of a package. The package can be main or not.
68
70
69
71
Every package except main should be a distinct folder on the `$GOPATH`. Main is a special package for which having a folder on `$GOPATH` is optional.
@@ -100,7 +102,7 @@ Here, we are in the Tasks/main directory, the binary will expect all the other f
100
102
101
103
There can be only _one_ main package & function per executable program. The main function takes no arguments passed and returns nothing.
In many cases, we do not know how many arguements can be passed, in such cases, we use variadic arguments.
300
300
@@ -306,7 +306,7 @@ for _, n := range arg {
306
306
fmt.Printf("And the number is: %d\n", n)
307
307
}
308
308
309
-
####Pass by value and pointers
309
+
### Pass by value and pointers
310
310
311
311
Argument are passed by value to the functions, the argument change inside the function doesn't affect the arguments used to call the function.
312
312
@@ -366,7 +366,7 @@ Advantages of pointers:
366
366
- Low cost by passing memory addresses (8 bytes), copy is not an efficient way, both in terms of time and space, to pass variables.
367
367
-`string`, `slice` and `map` are reference types, so they use pointers when passing to functions by default. (Attention: If you need to change the length of `slice`, you have to pass pointers explicitly)
368
368
369
-
####defer
369
+
### defer
370
370
371
371
Defer postpones the execution of a function till the calling function has finished executing. You can have many `defer` statements in one function; they will execute in reverse order when the program reaches its end. In the case where the program opens some resource files, these files would have to be closed before the function can return with errors. Let's see some examples.
372
372
@@ -413,7 +413,7 @@ for i := 0; i < 5; i++ {
413
413
}
414
414
```
415
415
416
-
####Functions as values and types
416
+
### Functions as values and types
417
417
418
418
Functions are also variables in Go, we can use `type` to define them. Functions that have the same signature can be seen as the same type.
419
419
@@ -465,7 +465,7 @@ func main(){
465
465
466
466
It's very useful when we use interfaces. As you can see `testInt` is a variable that has a function as type and the returned values and arguments of `filter` are the same as those of `testInt`. Therefore, we can have complex logic in our programs, while maintaining flexibility in our code.
467
467
468
-
####Panic and Recover
468
+
### Panic and Recover
469
469
470
470
Go doesn't have `try-catch` structure like Java does. Instead of throwing exceptions, Go uses `panic` and `recover` to deal with errors. However, you shouldn't use `panic` very much, although it's powerful.
Go has two retentions which are called `main` and `init`, where `init` can be used in all packages and `main` can only be used in the `main` package. These two functions are not able to have arguments or return values. Even though we can write many `init` functions in one package, I strongly recommend writing only one `init` function for each package.
505
505
@@ -509,7 +509,7 @@ Programs initialize and begin execution from the `main` package. If the `main` p
509
509
510
510

511
511
512
-
####import
512
+
### import
513
513
514
514
`import` is very often used in Go programs.
515
515
@@ -569,7 +569,7 @@ import (
569
569
570
570
The `_` operator actually means we just want to import that package and execute its `init` function, and we are not sure if want to use the functions belonging to that package.
Copy file name to clipboardexpand all lines: manuscript/02.4Struct.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ There are a lot of attributes of Product, its name, the ID used internally, the
22
22
-`isAvailable` is a `bool` which is true if the item is in stock, false otherwise.
23
23
-`inventoryLeft` is an `int` of the number of products left in stock.
24
24
25
-
####Initializing
25
+
### Initializing
26
26
27
27
```golang
28
28
// define goBook as a Product type
@@ -97,8 +97,8 @@ func main() {
97
97
98
98
}
99
99
```
100
-
101
-
#### embedded fields in struct
100
+
101
+
###Embedded fields in struct
102
102
103
103
In the earlier chapter, we saw how to define a struct with field names and type. Embedded fields can be thought of as subclass and superclass in Object oriented programming.
Copy file name to clipboardexpand all lines: manuscript/02.5ObjectOriented.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Object oriented languages allow programmers to declare a function inside the class definition. Go doesn't allow us to do that, we have to declare a method on a struct via a special syntax.
4
4
5
-
## methods
5
+
## Methods
6
6
7
7
We defined a "rectangle" struct and we want to calculate its area. Normally, we would create a function, pass the struct's instance and calculate the area.
8
8
@@ -98,7 +98,7 @@ One thing that's worth noting is that the method with a dotted line means the re
98
98
99
99
Any type can be the receiver of a method.
100
100
101
-
######Custom data types
101
+
## Custom data types
102
102
103
103
Use the following format to define a custom type.
104
104
@@ -220,7 +220,7 @@ Then we defined some methods for our customized types.
220
220
221
221
Is it much clearer when we use words to describe our requirements? We often write our requirements before we start coding.
222
222
223
-
####Use pointer as receiver
223
+
## Use pointer as receiver
224
224
225
225
Let's take a look at `SetColor` method. Its receiver is a pointer of Box. Yes, you can use `*Box` as a receiver. Why do we use a pointer here? Because we want to change Box's color in this method. Thus, if we don't use a pointer, it will only change the value inside a copy of Box.
226
226
@@ -230,7 +230,7 @@ You might be asking why we aren't using `(*b).Color=c` instead of `b.Color=c` in
230
230
231
231
You may also be asking whether we should use `(&bl[i]).SetColor(BLACK)` in `PaintItBlack` because we pass a pointer to `SetColor`. Again, either one is OK because Go knows how to interpret it!
232
232
233
-
####Inheritance of method
233
+
## Inheritance of method
234
234
235
235
We learned about inheritance of fields in the last section. Similarly, we also have method inheritance in Go. If an anonymous field has methods, then the struct that contains the field will have all the methods from it as well.
236
236
@@ -270,7 +270,7 @@ func main() {
270
270
}
271
271
```
272
272
273
-
####Method overload
273
+
## Method overload
274
274
275
275
If we want Employee to have its own method `SayHi`, we can define a method that has the same name in Employee, and it will hide `SayHi` in Human when we call it.
276
276
@@ -316,7 +316,7 @@ func main() {
316
316
317
317
Methods use rule of capital letter to decide whether public or private as well.
0 commit comments