Skip to content

Commit e80aacb

Browse files
committed
Update header levels and immediate spacing nearby
The spacing is throwing off formatting and the header levels affects content outlines.
1 parent d839b37 commit e80aacb

25 files changed

+172
-154
lines changed

manuscript/0.0installation.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Installation
2+
23
If you know about installation or have installed Go, you can skip to [Tools](0.1tools.md).
34

45
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.
56

6-
77
## System requirements
88

99
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
2121
‡You only need to install the command line tools for Xcode. If you have already installed Xcode 4.3+, you can
2222
install it from the Components tab of the Downloads preferences panel.
2323

24-
2524
## Install the Go tools
2625

27-
2826
If you are upgrading from an older version of Go you must first remove the existing version.
2927
Linux, Mac OS X, and FreeBSD tarballs
3028

@@ -40,7 +38,6 @@ Add` /usr/local/go/bin` to the PATH environment variable. You can do this by add
4038

4139
`export PATH=$PATH:/usr/local/go/bin`
4240

43-
4441
### Installing to a custom location
4542

4643
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
6966

7067
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.
7168

72-
7369
#### Zip archive
7470

7571
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.
115111

116112
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.
117113

118-
### Uninstalling Go
114+
## Uninstalling Go
119115

120116
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.
121117

122118
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.
123119

124-
### Getting help
120+
## Getting help
125121

126122
For real-time help, ask the helpful gophers in `#go-nuts` on the Freenode IRC server.
127123

128124
The official mailing list for discussion of the Go language is Go Nuts.
129125

130126
Report bugs using the Go issue tracker.
131127

132-
##### Links
128+
## Links
133129

134130
-[Next section](0.1tools.md)

manuscript/0.1tools.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,17 @@ We can build our application using `go build`. It parses all the `.go` files exc
6464

6565
Example: `go build -o tasks`
6666

67-
###### Build time
67+
#### Build time
6868

6969
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.
7070

71-
###### Cross compilation
71+
#### Cross compilation
7272

7373
Go allows cross compilation. We have to pass the OS name as linux/darwin/windows as GOOS as shown in the below commands.
7474

7575
env GOOS=darwin GOARCH=386 go build -o tasks.app
7676
env GOOS=windows GOARCH=386 go build -o tasks.exe
7777

78-
7978
### go install
8079

8180
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
9190

9291
The profile files are present in the home folder. Do a `cd ~` and check for either of the files mentioned above.
9392

94-
#### go run
93+
### go run
9594

9695
go run combines building and running the application in one command.
9796

9897
It generates a binary in the temp folder and executes it. The binary file isn't retained after the run.
9998

100-
#### go get
99+
### go get
101100

102101
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.
103102

104-
#### go clean
103+
### go clean
105104

106105
This command is for cleaning files that are generated by compilers, including the following files:
107106

@@ -116,7 +115,7 @@ This command is for cleaning files that are generated by compilers, including th
116115
DIR.test(.exe) // generated by go test -c
117116
MAINFILE(.exe) // generated by go build MAINFILE.go
118117

119-
#### Other commands
118+
### Other commands
120119

121120
Go provides more commands than those we've just talked about.
122121

@@ -127,7 +126,7 @@ Go provides more commands than those we've just talked about.
127126

128127
For details about specific commands, `go help <command>`.
129128

130-
##### Links
129+
### Links
131130

132131
-[Previous section](0.0installation.md)
133132
-[Next section](02.1IntroductionGo.md)

manuscript/02.1IntroductionGo.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ A list of features:
4242

4343
Let's start with the customary Hello World.
4444

45-
#### First Program
45+
### First Program
4646

4747
```golang
4848
package main
@@ -58,12 +58,14 @@ It prints following information.
5858

5959
Hello, world or καλημ ρα κóσμ
6060

61-
#### Explanation
61+
### Explanation
62+
6263
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.
6364

6465
We printed non ASCII characters. Go supports UTF-8 by default.
6566

66-
#### The main package
67+
### The main package
68+
6769
It is mandatory for each Go program to be a part of a package. The package can be main or not.
6870

6971
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
100102

101103
There can be only _one_ main package & function per executable program. The main function takes no arguments passed and returns nothing.
102104

103-
##### Links
105+
## Links
104106

105107
-[Previous section](0.1tools.md)
106108
-[Next section](02.2VariablesDataStruct.md)

manuscript/02.3CntrlStmtFunctions.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Control statements and Functions
22

3-
## Control statement
3+
## Control statements
44

55
### if
66

@@ -258,7 +258,7 @@ In a function call, if two or more arguments have the same data type, then we ca
258258

259259
`func max(a,b int, c,d string)`: this means we have four arguments, a,b: integers and c,d: string.
260260

261-
#### Multi-value return
261+
### Multi-value return
262262

263263
```golang
264264
package main
@@ -294,7 +294,7 @@ func SumAndProduct(A, B int) (add int, multiplied int) {
294294
// returns them
295295
```
296296

297-
#### Variadic arguments to functions
297+
### Variadic arguments to functions
298298

299299
In many cases, we do not know how many arguements can be passed, in such cases, we use variadic arguments.
300300

@@ -306,7 +306,7 @@ for _, n := range arg {
306306
fmt.Printf("And the number is: %d\n", n)
307307
}
308308

309-
#### Pass by value and pointers
309+
### Pass by value and pointers
310310

311311
Argument are passed by value to the functions, the argument change inside the function doesn't affect the arguments used to call the function.
312312

@@ -366,7 +366,7 @@ Advantages of pointers:
366366
- Low cost by passing memory addresses (8 bytes), copy is not an efficient way, both in terms of time and space, to pass variables.
367367
- `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)
368368

369-
#### defer
369+
### defer
370370

371371
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.
372372

@@ -413,7 +413,7 @@ for i := 0; i < 5; i++ {
413413
}
414414
```
415415

416-
#### Functions as values and types
416+
### Functions as values and types
417417

418418
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.
419419

@@ -465,7 +465,7 @@ func main(){
465465

466466
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.
467467

468-
#### Panic and Recover
468+
### Panic and Recover
469469

470470
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.
471471

@@ -499,7 +499,7 @@ func throwsPanic(f func()) (b bool) {
499499
}
500500
```
501501

502-
#### main and init functions
502+
### main and init functions
503503

504504
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.
505505

@@ -509,7 +509,7 @@ Programs initialize and begin execution from the `main` package. If the `main` p
509509

510510
![Flow of programs initialization](images/2.3.init.png)
511511

512-
#### import
512+
### import
513513

514514
`import` is very often used in Go programs.
515515

@@ -569,7 +569,7 @@ import (
569569

570570
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.
571571

572-
##### Links
572+
### Links
573573

574574
-[Previous section](02.2VariablesDataStruct.md)
575575
-[Next section](02.4Struct.md)

manuscript/02.4Struct.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ There are a lot of attributes of Product, its name, the ID used internally, the
2222
- `isAvailable` is a `bool` which is true if the item is in stock, false otherwise.
2323
- `inventoryLeft` is an `int` of the number of products left in stock.
2424

25-
#### Initializing
25+
### Initializing
2626

2727
```golang
2828
// define goBook as a Product type
@@ -97,8 +97,8 @@ func main() {
9797

9898
}
9999
```
100-
101-
#### embedded fields in struct
100+
101+
### Embedded fields in struct
102102

103103
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.
104104

@@ -233,7 +233,7 @@ func main() {
233233
}
234234
```
235235

236-
##### Links
236+
## Links
237237

238238
-[Previous section](02.3CntrlStmtFunctions.md)
239239
-[Next section](02.5ObjectOriented.md)

manuscript/02.5ObjectOriented.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

5-
## methods
5+
## Methods
66

77
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.
88

@@ -98,7 +98,7 @@ One thing that's worth noting is that the method with a dotted line means the re
9898

9999
Any type can be the receiver of a method.
100100

101-
###### Custom data types
101+
## Custom data types
102102

103103
Use the following format to define a custom type.
104104

@@ -220,7 +220,7 @@ Then we defined some methods for our customized types.
220220

221221
Is it much clearer when we use words to describe our requirements? We often write our requirements before we start coding.
222222

223-
#### Use pointer as receiver
223+
## Use pointer as receiver
224224

225225
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.
226226

@@ -230,7 +230,7 @@ You might be asking why we aren't using `(*b).Color=c` instead of `b.Color=c` in
230230

231231
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!
232232

233-
#### Inheritance of method
233+
## Inheritance of method
234234

235235
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.
236236

@@ -270,7 +270,7 @@ func main() {
270270
}
271271
```
272272

273-
#### Method overload
273+
## Method overload
274274

275275
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.
276276

@@ -316,7 +316,7 @@ func main() {
316316

317317
Methods use rule of capital letter to decide whether public or private as well.
318318

319-
##### Links
319+
## Links
320320

321321
-[Previous section](02.4Struct.md)
322322
-[Next section](02.6Interface.md)

0 commit comments

Comments
 (0)