Skip to content

Commit 5315ab3

Browse files
committed
adding minor changes
1 parent 2416d72 commit 5315ab3

File tree

3 files changed

+111
-108
lines changed

3 files changed

+111
-108
lines changed

en/02.1.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ The Go programming language was created with one goal in mind, to be able to bui
66
public static void main() {
77

88
}
9-
10-
or
11-
public static void main()
9+
10+
or
11+
public static void main()
1212
{
1313

1414
}
@@ -21,13 +21,13 @@ For other languages there are many variables when it comes to writing code, ever
2121

2222
So to mitigate all the problems that Google faced with the current tools, they wrote a systems language called Go, which you are about to learn! There are many advantages to using golang, and there might be disadvantages too for every coin has both sides. But significant improvements in places like code formatting, since they designed the language in such a way that there won't be wars on how to format code, the gocode written by anyone in the world (assuming they know and use `gofmt`) will look exactly the same, this won't seem to matter until you work in a team! also when the company uses automated code review or some other fancy technique then in other languages which don't have strict and standard formatting rules then the code might get screwed up, but not in go!
2323

24-
Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, blog.golang.org, you will find it there, it is worth a read.
24+
Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, [www.blog.golang.org](https://blog.golang.org/concurrency-is-not-parallelism), you will find it there, it is worth a read.
2525

2626
Another very important change that go has brought in programming that I personally love is the concept of `GOPATH`, gone are the days when you had to create a folder called `code` and then create workspaces for eclipse and what not, now you have to keep one folder tree for go code and it'll be updated by the package manager automatically. Also under the code we are recommended to create folders with either a custom domain or the github domain, for example I created a task manager using golang so I created a set of folders
2727
`~/go/src/github.com/thewhitetulip/Tasks` note: In *nix systems `~` stands for home directory, which is the windows equivalent of `C:\\Users\\username`
2828
now the `~/go/` is the universe for the gocode in your machine, it is just a significant improvement over other languages so we can store the code efficiently without hassles, it might seem strange at first, but it does make a lot of sense over the ridiculous package names some other languages use like reverse domains.
2929

30-
note: along with src there are two folders `pkg` which is for packages and `bin` which is for binary
30+
**Note:** Along with `src` there are two folders `pkg` which is for packages and `bin` which is for binary.
3131

3232
This `GOPATH` advantage isn't just restricted to storing code in particular folder, but when you have created five packages for your project then you don't have to import them like `"import ./db"`, you can give it `import "github.com/thewhitetulip/Tasks/db"`, so while doing a `go get` on my repo, the `go` tool will find the package from `github.com/...` path if it wasn't downloaded initially, it just standardizes a lot of screwed up things in the programming discipline.
3333

@@ -44,17 +44,17 @@ According to international practice, before you learn how to program in some lan
4444
Are you ready? Let's Go!
4545
```Go
4646
package main
47-
47+
4848
import "fmt"
49-
49+
5050
func main() {
5151
fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
5252
}
53-
```
53+
```
5454
It prints following information.
5555

5656
Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界
57-
57+
5858
## Explanation
5959

6060
One thing that you should know in the first is that Go programs are composed by `package`.
@@ -75,7 +75,7 @@ On the sixth line, we called the function `Printf` which is from the package `fm
7575

7676
As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the `<pkgName>` comes from the name in `package <pkgName>`, not the folder's name.
7777

78-
You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.
78+
You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.
7979

8080
Each go file is in some package, and that package should be a distinct folder in the GOPATH, but main is a special package which doesn't require a `main` folder. This is one aspect which they left out for standardization! But should you choose to make a main folder then you have to ensure that you run the binary properly. Also one go code can't have more than one `main` go file.
8181

en/02.2.md

+45-45
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,60 @@ Define multiple variables.
1515
```Go
1616
// define three variables which types are "type"
1717
var vname1, vname2, vname3 type
18-
```
18+
```
1919
Define a variable with initial value.
2020
```Go
2121
// define a variable with name “variableName”, type "type" and value "value"
2222
var variableName type = value
23-
```
23+
```
2424
Define multiple variables with initial values.
2525
```Go
2626
/*
2727
Define three variables with type "type", and initialize their values.
2828
vname1 is v1, vname2 is v2, vname3 is v3
2929
*/
3030
var vname1, vname2, vname3 type = v1, v2, v3
31-
```
31+
```
3232
Do you think that it's too tedious to define variables use the way above? Don't worry, because the Go team has also found
33-
this to be a problem. Therefore if you want to define variables with initial values, we can just omit the variable type,
33+
this to be a problem. Therefore if you want to define variables with initial values, we can just omit the variable type,
3434
so the code will look like this instead:
3535
```Go
3636
/*
3737
Define three variables without type "type", and initialize their values.
3838
vname1 is v1,vname2 is v2,vname3 is v3
3939
*/
4040
var vname1, vname2, vname3 = v1, v2, v3
41-
```
41+
```
4242
Well, I know this is still not simple enough for you. Let's see how we fix it.
4343
```Go
4444
/*
4545
Define three variables without type "type" and without keyword "var", and initialize their values.
4646
vname1 is v1,vname2 is v2,vname3 is v3
4747
*/
4848
vname1, vname2, vname3 := v1, v2, v3
49-
```
50-
Now it looks much better. Use `:=` to replace `var` and `type`, this is called a brief statement. But wait, it has one limitation: this form can only be used inside of functions. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables.
49+
```
50+
Now it looks much better. Use `:=` to replace `var` and `type`, this is called a **short assignment**. It has one limitation: this form can only be used inside of a functions. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables.
5151

5252
`_` (blank) is a special variable name. Any value that is given to it will be ignored. For example, we give `35` to `b`, and discard `34`.( ***This example just show you how it works. It looks useless here because we often use this symbol when we get function return values.*** )
5353
```Go
5454
_, b := 34, 35
55-
```
55+
```
5656
If you don't use variables that you've defined in your program, the compiler will give you compilation errors. Try to compile the following code and see what happens.
5757
```Go
5858
package main
5959

6060
func main() {
6161
var i int
6262
}
63-
```
63+
```
6464
## Constants
6565

6666
So-called constants are the values that are determined during compile time and you cannot change them during runtime. In Go, you can use number, boolean or string as types of constants.
6767

6868
Define constants as follows.
6969
```Go
7070
const constantName = value
71-
// you can assign type of constants if it's necessary
71+
// you can assign type of constants if it's necessary
7272
const Pi float32 = 3.1415926
7373
```
7474
More examples.
@@ -77,7 +77,7 @@ const Pi = 3.1415926
7777
const i = 10000
7878
const MaxThread = 10
7979
const prefix = "astaxie_"
80-
```
80+
```
8181
## Elementary types
8282

8383
### Boolean
@@ -93,7 +93,7 @@ func test() {
9393
valid := false // brief statement of variable
9494
available = true // assign value to variable
9595
}
96-
```
96+
```
9797
### Numerical types
9898

9999
Integer types include both signed and unsigned integer types. Go has `int` and `uint` at the same time, they have same length, but specific length depends on your operating system. They use 32-bit in 32-bit operating systems, and 64-bit in 64-bit operating systems. Go also has types that have specific length including `rune`, `int8`, `int16`, `int32`, `int64`, `byte`, `uint8`, `uint16`, `uint32`, `uint64`. Note that `rune` is alias of `int32` and `byte` is alias of `uint8`.
@@ -115,7 +115,7 @@ That's all? No! Go supports complex numbers as well. `complex128` (with a 64-bit
115115
var c complex64 = 5+5i
116116
//output: (5+5i)
117117
fmt.Printf("Value is: %v", c)
118-
```
118+
```
119119
### String
120120

121121
We just talked about how Go uses the UTF-8 character set. Strings are represented by double quotes `""` or backticks ``` `` ```.
@@ -133,28 +133,28 @@ It's impossible to change string values by index. You will get errors when you c
133133
```Go
134134
var s string = "hello"
135135
s[0] = 'c'
136-
```
136+
```
137137
What if I really want to change just one character in a string? Try the following code.
138138
```Go
139139
s := "hello"
140140
c := []byte(s) // convert string to []byte type
141141
c[0] = 'c'
142142
s2 := string(c) // convert back to string type
143143
fmt.Printf("%s\n", s2)
144-
```
144+
```
145145
You use the `+` operator to combine two strings.
146146
```Go
147147
s := "hello,"
148148
m := " world"
149149
a := s + m
150150
fmt.Printf("%s\n", a)
151-
```
151+
```
152152
and also.
153153
```Go
154154
s := "hello"
155155
s = "c" + s[1:] // you cannot change string values by index, but you can get values instead.
156156
fmt.Printf("%s\n", s)
157-
```
157+
```
158158
What if I want to have a multiple-line string?
159159
```Go
160160
m := `hello
@@ -170,7 +170,7 @@ err := errors.New("emit macho dwarf: elf header corrupted")
170170
if err != nil {
171171
fmt.Print(err)
172172
}
173-
```
173+
```
174174
### Underlying data structure
175175

176176
The following picture comes from an article about [Go data structure](http://research.swtch.com/godata) in [Russ Cox's Blog](http://research.swtch.com/). As you can see, Go utilizes blocks of memory to store data.
@@ -197,7 +197,7 @@ const prefix = "Go_"
197197
var i int
198198
var pi float32
199199
var prefix string
200-
```
200+
```
201201
Group form.
202202
```Go
203203
import(
@@ -216,7 +216,7 @@ var(
216216
pi float32
217217
prefix string
218218
)
219-
```
219+
```
220220
Unless you assign the value of constant is `iota`, the first value of constant in the group `const()` will be `0`. If following constants don't assign values explicitly, their values will be the same as the last one. If the value of last constant is `iota`, the values of following constants which are not assigned are `iota` also.
221221

222222
### iota enumerate
@@ -227,13 +227,13 @@ const(
227227
x = iota // x == 0
228228
y = iota // y == 1
229229
z = iota // z == 2
230-
w // If there is no expression after the constants name, it uses the last expression,
230+
w // If there is no expression after the constants name, it uses the last expression,
231231
//so it's saying w = iota implicitly. Therefore w == 3, and y and z both can omit "= iota" as well.
232232
)
233233

234234
const v = iota // once iota meets keyword `const`, it resets to `0`, so v = 0.
235235

236-
const (
236+
const (
237237
e, f, g = iota, iota, iota // e=0,f=0,g=0 values of iota are same in one line.
238238
)
239239
```
@@ -251,25 +251,25 @@ The reason that Go is concise because it has some default behaviors.
251251
`array` is an array obviously, we define one as follows.
252252
```Go
253253
var arr [n]type
254-
```
254+
```
255255
in `[n]type`, `n` is the length of the array, `type` is the type of its elements. Like other languages, we use `[]` to get or set element values within arrays.
256256
```Go
257257
var arr [10]int // an array of type [10]int
258258
arr[0] = 42 // array is 0-based
259259
arr[1] = 13 // assign value to element
260260
fmt.Printf("The first element is %d\n", arr[0])
261261
// get element value, it returns 42
262-
fmt.Printf("The last element is %d\n", arr[9])
262+
fmt.Printf("The last element is %d\n", arr[9])
263263
//it returns default value of 10th element in this array, which is 0 in this case.
264-
```
264+
```
265265
Because length is a part of the array type, `[3]int` and `[4]int` are different types, so we cannot change the length of arrays. When you use arrays as arguments, functions get their copies instead of references! If you want to use references, you may want to use `slice`. We'll talk about later.
266266

267267
It's possible to use `:=` when you define arrays.
268268
```Go
269269
a := [3]int{1, 2, 3} // define an int array with 3 elements
270270

271-
b := [10]int{1, 2, 3}
272-
// define a int array with 10 elements, of which the first three are assigned.
271+
b := [10]int{1, 2, 3}
272+
// define a int array with 10 elements, of which the first three are assigned.
273273
//The rest of them use the default value 0.
274274

275275
c := [...]int{4, 5, 6} // use `…` to replace the length parameter and Go will calculate it for you.
@@ -281,7 +281,7 @@ doubleArray := [2][4]int{[4]int{1, 2, 3, 4}, [4]int{5, 6, 7, 8}}
281281

282282
// The declaration can be written more concisely as follows.
283283
easyArray := [2][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
284-
```
284+
```
285285
Array underlying data structure.
286286

287287
![](images/2.2.array.png?raw=true)
@@ -300,8 +300,8 @@ var fslice []int
300300
Then we define a `slice`, and initialize its data.
301301
```Go
302302
slice := []byte {'a', 'b', 'c', 'd'}
303-
```
304-
`slice` can redefine existing slices or arrays. `slice` uses `array[i:j]` to slice, where `i` is
303+
```
304+
`slice` can redefine existing slices or arrays. `slice` uses `array[i:j]` to slice, where `i` is
305305
the start index and `j` is end index, but notice that `array[j]` will not be sliced since the length
306306
of the slice is `j-i`.
307307
```Go
@@ -318,8 +318,8 @@ a = ar[2:5]
318318
// 'b' is another slice of array ar
319319
b = ar[3:5]
320320
// now 'b' has elements ar[3] and ar[4]
321-
```
322-
Notice the differences between `slice` and `array` when you define them. We use `[…]` to let Go
321+
```
322+
Notice the differences between `slice` and `array` when you define them. We use `[…]` to let Go
323323
calculate length but use `[]` to define slice only.
324324

325325
Their underlying data structure.
@@ -353,8 +353,8 @@ bSlice = aSlice[:3] // bSlice contains aSlice[0], aSlice[1], aSlice[2], so it h
353353
bSlice = aSlice[0:5] // slice could be expanded in range of cap, now bSlice contains d,e,f,g,h
354354
bSlice = aSlice[:] // bSlice has same elements as aSlice does, which are d,e,f,g
355355
```
356-
`slice` is a reference type, so any changes will affect other variables pointing to the same slice or array.
357-
For instance, in the case of `aSlice` and `bSlice` above, if you change the value of an element in `aSlice`,
356+
`slice` is a reference type, so any changes will affect other variables pointing to the same slice or array.
357+
For instance, in the case of `aSlice` and `bSlice` above, if you change the value of an element in `aSlice`,
358358
`bSlice` will be changed as well.
359359

360360
`slice` is like a struct by definition and it contains 3 parts.
@@ -379,24 +379,24 @@ There are some built-in functions for slice.
379379
- `append` appends one or more elements to `slice`, and returns `slice` .
380380
- `copy` copies elements from one slice to the other, and returns the number of elements that were copied.
381381

382-
Attention: `append` will change the array that `slice` points to, and affect other slices that point to the same array.
383-
Also, if there is not enough length for the slice (`(cap-len) == 0`), `append` returns a new array for this slice. When
382+
Attention: `append` will change the array that `slice` points to, and affect other slices that point to the same array.
383+
Also, if there is not enough length for the slice (`(cap-len) == 0`), `append` returns a new array for this slice. When
384384
this happens, other slices pointing to the old array will not be affected.
385385

386386
### map
387387

388388
`map` behaves like a dictionary in Python. Use the form `map[keyType]valueType` to define it.
389389

390390
Let's see some code. The 'set' and 'get' values in `map` are similar to `slice`, however the index in `slice` can only be
391-
of type 'int' while `map` can use much more than that: for example `int`, `string`, or whatever you want. Also, they are
391+
of type 'int' while `map` can use much more than that: for example `int`, `string`, or whatever you want. Also, they are
392392
all able to use `==` and `!=` to compare values.
393393
```Go
394394
// use string as the key type, int as the value type, and `make` initialize it.
395395
var numbers map[string] int
396396
// another way to define map
397397
numbers := make(map[string]int)
398398
numbers["one"] = 1 // assign value by key
399-
numbers["ten"] = 10
399+
numbers["ten"] = 10
400400
numbers["three"] = 3
401401

402402
fmt.Println("The third number is: ", numbers["three"]) // get values
@@ -415,7 +415,7 @@ Use `delete` to delete an element in `map`.
415415
```Go
416416
// Initialize a map
417417
rating := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
418-
// map has two return values. For the second return value, if the key doesn't
418+
// map has two return values. For the second return value, if the key doesn't
419419
//exist,'ok' returns false. It returns true otherwise.
420420
csharpRating, ok := rating["C#"]
421421
if ok {
@@ -426,25 +426,25 @@ if ok {
426426

427427
delete(rating, "C") // delete element with key "c"
428428
```
429-
As I said above, `map` is a reference type. If two `map`s point to same underlying data,
429+
As I said above, `map` is a reference type. If two `map`s point to same underlying data,
430430
any change will affect both of them.
431431
```Go
432432
m := make(map[string]string)
433433
m["Hello"] = "Bonjour"
434434
m1 := m
435435
m1["Hello"] = "Salut" // now the value of m["hello"] is Salut
436-
```
436+
```
437437
### make, new
438438

439-
`make` does memory allocation for built-in models, such as `map`, `slice`, and `channel`, while `new` is for types'
439+
`make` does memory allocation for built-in models, such as `map`, `slice`, and `channel`, while `new` is for types'
440440
memory allocation.
441441

442-
`new(T)` allocates zero-value to type `T`'s memory, returns its memory address, which is the value of type `*T`. By Go's
442+
`new(T)` allocates zero-value to type `T`'s memory, returns its memory address, which is the value of type `*T`. By Go's
443443
definition, it returns a pointer which points to type `T`'s zero-value.
444444

445445
`new` returns pointers.
446446

447-
The built-in function `make(T, args)` has different purposes than `new(T)`. `make` can be used for `slice`, `map`,
447+
The built-in function `make(T, args)` has different purposes than `new(T)`. `make` can be used for `slice`, `map`,
448448
and `channel`, and returns a type `T` with an initial value. The reason for doing this is because the underlying data of
449449
these three types must be initialized before they point to them. For example, a `slice` contains a pointer that points to
450450
the underlying `array`, length and capacity. Before these data are initialized, `slice` is `nil`, so for `slice`, `map`
@@ -471,7 +471,7 @@ float32 0 // length is 4 byte
471471
float64 0 //length is 8 byte
472472
bool false
473473
string ""
474-
```
474+
```
475475
## Links
476476

477477
- [Directory](preface.md)

0 commit comments

Comments
 (0)