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.4.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -33,15 +33,15 @@ There are three more ways to define a struct.
33
33
34
34
- Assign initial values by order
35
35
```Go
36
-
P:= person{"Tom", 25}
36
+
P:= person{"Tom", 25}
37
37
```
38
38
- Use the format `field:value` to initialize the struct without order
39
39
```Go
40
-
P:= person{age:24, name:"Bob"}
40
+
P:= person{age:24, name:"Bob"}
41
41
```
42
42
- Define an anonymous struct, then initialize it
43
43
```Go
44
-
P:=struct{name string; age int}{"Amy",18}
44
+
P:=struct{name string; age int}{"Amy",18}
45
45
```
46
46
Let's see a complete example.
47
47
```Go
@@ -141,8 +141,8 @@ Figure 2.7 Embedding in Student and Human
141
141
142
142
We see that we can access the `age` and `name` fields in Student just like we can in Human. This is how embedded fields work. It's very cool, isn't it? Hold on, there's something cooler! You can even use Student to access Human in this embedded field!
143
143
```Go
144
-
mark.Human = Human{"Marcus", 55, 220}
145
-
mark.Human.age -= 1
144
+
mark.Human = Human{"Marcus", 55, 220}
145
+
mark.Human.age -= 1
146
146
```
147
147
All the types in Go can be used as embedded fields.
Copy file name to clipboardExpand all lines: en/02.6.md
+23-23Lines changed: 23 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -175,13 +175,13 @@ An interface is a set of abstract methods, and can be implemented by non-interfa
175
175
176
176
An empty interface is an interface that doesn't contain any methods, so all types implement an empty interface. This fact is very useful when we want to store all types at some point, and is similar to void* in C.
177
177
```Go
178
-
// define a as empty interface
179
-
varainterface{}
180
-
variint = 5
181
-
s:="Hello world"
182
-
// a can store value of any type
183
-
a = i
184
-
a = s
178
+
// define a as empty interface
179
+
varainterface{}
180
+
variint = 5
181
+
s:="Hello world"
182
+
// a can store value of any type
183
+
a = i
184
+
a = s
185
185
```
186
186
If a function uses an empty interface as its argument type, it can accept any type; if a function uses empty interface as its return value type, it can return any type.
187
187
@@ -223,8 +223,8 @@ func main() {
223
223
```
224
224
Looking back to the example of Box, you will find that Color implements interface Stringer as well, so we are able to customize the print format. If we don't implement this interface, fmt.Println prints the type with its default format.
225
225
```Go
226
-
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
227
-
fmt.Println("The biggest one is", boxes.BiggestsColor())
226
+
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
227
+
fmt.Println("The biggest one is", boxes.BiggestsColor())
228
228
```
229
229
Attention: If the type implemented the interface `error`, fmt will call `Error()`, so you don't have to implement Stringer at this point.
230
230
@@ -366,29 +366,29 @@ Reflection in Go is used for determining information at runtime. We use the `ref
366
366
367
367
There are three steps involved when using reflect. First, we need to convert an interface to reflect types (reflect.Type or reflect.Value, this depends on the situation).
368
368
```Go
369
-
t:= reflect.TypeOf(i) // get meta-data in type i, and use t to get all elements
370
-
v:= reflect.ValueOf(i) // get actual value in type i, and use v to change its value
369
+
t:= reflect.TypeOf(i) // get meta-data in type i, and use t to get all elements
370
+
v:= reflect.ValueOf(i) // get actual value in type i, and use v to change its value
371
371
```
372
372
After that, we can convert the reflected types to get the values that we need.
373
373
```Go
374
-
varxfloat64 = 3.4
375
-
v:= reflect.ValueOf(x)
376
-
fmt.Println("type:", v.Type())
377
-
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
378
-
fmt.Println("value:", v.Float())
374
+
varxfloat64 = 3.4
375
+
v:= reflect.ValueOf(x)
376
+
fmt.Println("type:", v.Type())
377
+
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
378
+
fmt.Println("value:", v.Float())
379
379
```
380
380
Finally, if we want to change the values of the reflected types, we need to make it modifiable. As discussed earlier, there is a difference between pass by value and pass by reference. The following code will not compile.
381
381
```Go
382
-
varxfloat64 = 3.4
383
-
v:= reflect.ValueOf(x)
384
-
v.SetFloat(7.1)
382
+
varxfloat64 = 3.4
383
+
v:= reflect.ValueOf(x)
384
+
v.SetFloat(7.1)
385
385
```
386
386
Instead, we must use the following code to change the values from reflect types.
387
387
```Go
388
-
varxfloat64 = 3.4
389
-
p:= reflect.ValueOf(&x)
390
-
v:= p.Elem()
391
-
v.SetFloat(7.1)
388
+
varxfloat64 = 3.4
389
+
p:= reflect.ValueOf(&x)
390
+
v:= p.Elem()
391
+
v.SetFloat(7.1)
392
392
```
393
393
We have just discussed the basics of reflection, however you must practice more in order to understand more.
Copy file name to clipboardExpand all lines: en/02.7.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ goroutines and concurrency are built into the core design of Go. They're similar
8
8
9
9
goroutines run on the thread manager at runtime in Go. We use the `go` keyword to create a new goroutine, which is a function at the underlying level ( ***main() is a goroutine*** ).
10
10
```Go
11
-
gohello(a, b, c)
11
+
gohello(a, b, c)
12
12
```
13
13
Let's see an example.
14
14
```Go
@@ -56,14 +56,14 @@ Before Go 1.5,The scheduler only uses one thread to run all goroutines, which me
56
56
57
57
goroutines run in the same memory address space, so you have to maintain synchronization when you want to access shared memory. How do you communicate between different goroutines? Go uses a very good communication mechanism called `channel`. `channel` is like a two-way pipeline in Unix shells: use `channel` to send or receive data. The only data type that can be used in channels is the type `channel` and the keyword `chan`. Be aware that you have to use `make` to create a new `channel`.
58
58
```Go
59
-
ci:=make(chanint)
60
-
cs:=make(chanstring)
61
-
cf:=make(chaninterface{})
59
+
ci:=make(chanint)
60
+
cs:=make(chanstring)
61
+
cf:=make(chaninterface{})
62
62
```
63
63
channel uses the operator `<-` to send or receive data.
64
64
```Go
65
-
ch <- v // send v to channel ch.
66
-
v:=<-ch // receive data from ch, and assign to v
65
+
ch <- v // send v to channel ch.
66
+
v:=<-ch // receive data from ch, and assign to v
67
67
```
68
68
Let's see more examples.
69
69
```Go
@@ -97,10 +97,10 @@ Sending and receiving data in channels blocks by default, so it's much easier to
97
97
98
98
I introduced non-buffered channels above. Go also has buffered channels that can store more than a single element. For example, `ch := make(chan bool, 4)`, here we create a channel that can store 4 boolean elements. So in this channel, we are able to send 4 elements into it without blocking, but the goroutine will be blocked when you try to send a fifth element and no goroutine receives it.
99
99
```Go
100
-
ch:=make(chantype, n)
100
+
ch:=make(chantype, n)
101
101
102
-
n == 0 ! non-buffer(block)
103
-
n > 0 ! buffer(non-block until n elements in the channel)
102
+
n == 0 ! non-buffer(block)
103
+
n > 0 ! buffer(non-block until n elements in the channel)
104
104
```
105
105
You can try the following code on your computer and change some values.
0 commit comments