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: en/02.1.md
+10-10
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ The Go programming language was created with one goal in mind, to be able to bui
6
6
publicstaticvoid main() {
7
7
8
8
}
9
-
10
-
or
11
-
publicstaticvoid main()
9
+
10
+
or
11
+
publicstaticvoid main()
12
12
{
13
13
14
14
}
@@ -21,13 +21,13 @@ For other languages there are many variables when it comes to writing code, ever
21
21
22
22
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!
23
23
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.
25
25
26
26
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
27
27
`~/go/src/github.com/thewhitetulip/Tasks` note: In *nix systems `~` stands for home directory, which is the windows equivalent of `C:\\Users\\username`
28
28
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.
29
29
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.
31
31
32
32
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.
33
33
@@ -44,17 +44,17 @@ According to international practice, before you learn how to program in some lan
44
44
Are you ready? Let's Go!
45
45
```Go
46
46
package main
47
-
47
+
48
48
import"fmt"
49
-
49
+
50
50
funcmain() {
51
51
fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
52
52
}
53
-
```
53
+
```
54
54
It prints following information.
55
55
56
56
Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界
57
-
57
+
58
58
## Explanation
59
59
60
60
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
75
75
76
76
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.
77
77
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.
79
79
80
80
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.
Copy file name to clipboardExpand all lines: en/02.2.md
+45-45
Original file line number
Diff line number
Diff line change
@@ -15,60 +15,60 @@ Define multiple variables.
15
15
```Go
16
16
// define three variables which types are "type"
17
17
varvname1, vname2, vname3type
18
-
```
18
+
```
19
19
Define a variable with initial value.
20
20
```Go
21
21
// define a variable with name “variableName”, type "type" and value "value"
22
22
varvariableNametype = value
23
-
```
23
+
```
24
24
Define multiple variables with initial values.
25
25
```Go
26
26
/*
27
27
Define three variables with type "type", and initialize their values.
28
28
vname1 is v1, vname2 is v2, vname3 is v3
29
29
*/
30
30
varvname1, vname2, vname3type = v1, v2, v3
31
-
```
31
+
```
32
32
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,
34
34
so the code will look like this instead:
35
35
```Go
36
36
/*
37
37
Define three variables without type "type", and initialize their values.
38
38
vname1 is v1,vname2 is v2,vname3 is v3
39
39
*/
40
40
varvname1, vname2, vname3 = v1, v2, v3
41
-
```
41
+
```
42
42
Well, I know this is still not simple enough for you. Let's see how we fix it.
43
43
```Go
44
44
/*
45
45
Define three variables without type "type" and without keyword "var", and initialize their values.
46
46
vname1 is v1,vname2 is v2,vname3 is v3
47
47
*/
48
48
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.
51
51
52
52
`_` (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.*** )
53
53
```Go
54
54
_, b:=34, 35
55
-
```
55
+
```
56
56
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.
57
57
```Go
58
58
package main
59
59
60
60
funcmain() {
61
61
variint
62
62
}
63
-
```
63
+
```
64
64
## Constants
65
65
66
66
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.
67
67
68
68
Define constants as follows.
69
69
```Go
70
70
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
72
72
constPifloat32 = 3.1415926
73
73
```
74
74
More examples.
@@ -77,7 +77,7 @@ const Pi = 3.1415926
77
77
const i = 10000
78
78
constMaxThread = 10
79
79
const prefix = "astaxie_"
80
-
```
80
+
```
81
81
## Elementary types
82
82
83
83
### Boolean
@@ -93,7 +93,7 @@ func test() {
93
93
valid:=false// brief statement of variable
94
94
available = true// assign value to variable
95
95
}
96
-
```
96
+
```
97
97
### Numerical types
98
98
99
99
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
115
115
varccomplex64 = 5+5i
116
116
//output: (5+5i)
117
117
fmt.Printf("Value is: %v", c)
118
-
```
118
+
```
119
119
### String
120
120
121
121
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
133
133
```Go
134
134
varsstring = "hello"
135
135
s[0] = 'c'
136
-
```
136
+
```
137
137
What if I really want to change just one character in a string? Try the following code.
138
138
```Go
139
139
s:="hello"
140
140
c:= []byte(s) // convert string to []byte type
141
141
c[0] = 'c'
142
142
s2:=string(c) // convert back to string type
143
143
fmt.Printf("%s\n", s2)
144
-
```
144
+
```
145
145
You use the `+` operator to combine two strings.
146
146
```Go
147
147
s:="hello,"
148
148
m:=" world"
149
149
a:= s + m
150
150
fmt.Printf("%s\n", a)
151
-
```
151
+
```
152
152
and also.
153
153
```Go
154
154
s:="hello"
155
155
s = "c" + s[1:] // you cannot change string values by index, but you can get values instead.
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_"
197
197
variint
198
198
varpifloat32
199
199
varprefixstring
200
-
```
200
+
```
201
201
Group form.
202
202
```Go
203
203
import(
@@ -216,7 +216,7 @@ var(
216
216
pi float32
217
217
prefix string
218
218
)
219
-
```
219
+
```
220
220
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.
221
221
222
222
### iota enumerate
@@ -227,13 +227,13 @@ const(
227
227
x = iota// x == 0
228
228
y = iota// y == 1
229
229
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,
231
231
//so it's saying w = iota implicitly. Therefore w == 3, and y and z both can omit "= iota" as well.
232
232
)
233
233
234
234
const v = iota// once iota meets keyword `const`, it resets to `0`, so v = 0.
235
235
236
-
const (
236
+
const (
237
237
e, f, g = iota, iota, iota// e=0,f=0,g=0 values of iota are same in one line.
238
238
)
239
239
```
@@ -251,25 +251,25 @@ The reason that Go is concise because it has some default behaviors.
251
251
`array` is an array obviously, we define one as follows.
252
252
```Go
253
253
vararr [n]type
254
-
```
254
+
```
255
255
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.
256
256
```Go
257
257
vararr [10]int// an array of type [10]int
258
258
arr[0] = 42// array is 0-based
259
259
arr[1] = 13// assign value to element
260
260
fmt.Printf("The first element is %d\n", arr[0])
261
261
// 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])
263
263
//it returns default value of 10th element in this array, which is 0 in this case.
264
-
```
264
+
```
265
265
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.
266
266
267
267
It's possible to use `:=` when you define arrays.
268
268
```Go
269
269
a:= [3]int{1, 2, 3} // define an int array with 3 elements
270
270
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.
273
273
//The rest of them use the default value 0.
274
274
275
275
c:= [...]int{4, 5, 6} // use `…` to replace the length parameter and Go will calculate it for you.
0 commit comments