Skip to content

Commit c93ed67

Browse files
committed
Format and remove en/02.x.md spaces
1 parent 2859b37 commit c93ed67

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

en/02.4.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ There are three more ways to define a struct.
3333

3434
- Assign initial values by order
3535
```Go
36-
P := person{"Tom", 25}
36+
P := person{"Tom", 25}
3737
```
3838
- Use the format `field:value` to initialize the struct without order
3939
```Go
40-
P := person{age:24, name:"Bob"}
40+
P := person{age:24, name:"Bob"}
4141
```
4242
- Define an anonymous struct, then initialize it
4343
```Go
44-
P := struct{name string; age int}{"Amy",18}
44+
P := struct{name string; age int}{"Amy",18}
4545
```
4646
Let's see a complete example.
4747
```Go
@@ -141,8 +141,8 @@ Figure 2.7 Embedding in Student and Human
141141

142142
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!
143143
```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
146146
```
147147
All the types in Go can be used as embedded fields.
148148
```Go

en/02.6.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ An interface is a set of abstract methods, and can be implemented by non-interfa
175175

176176
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.
177177
```Go
178-
// define a as empty interface
179-
var a interface{}
180-
var i int = 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+
var a interface{}
180+
var i int = 5
181+
s := "Hello world"
182+
// a can store value of any type
183+
a = i
184+
a = s
185185
```
186186
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.
187187

@@ -223,8 +223,8 @@ func main() {
223223
```
224224
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.
225225
```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())
228228
```
229229
Attention: If the type implemented the interface `error`, fmt will call `Error()`, so you don't have to implement Stringer at this point.
230230

@@ -366,29 +366,29 @@ Reflection in Go is used for determining information at runtime. We use the `ref
366366

367367
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).
368368
```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
371371
```
372372
After that, we can convert the reflected types to get the values that we need.
373373
```Go
374-
var x float64 = 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+
var x float64 = 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())
379379
```
380380
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.
381381
```Go
382-
var x float64 = 3.4
383-
v := reflect.ValueOf(x)
384-
v.SetFloat(7.1)
382+
var x float64 = 3.4
383+
v := reflect.ValueOf(x)
384+
v.SetFloat(7.1)
385385
```
386386
Instead, we must use the following code to change the values from reflect types.
387387
```Go
388-
var x float64 = 3.4
389-
p := reflect.ValueOf(&x)
390-
v := p.Elem()
391-
v.SetFloat(7.1)
388+
var x float64 = 3.4
389+
p := reflect.ValueOf(&x)
390+
v := p.Elem()
391+
v.SetFloat(7.1)
392392
```
393393
We have just discussed the basics of reflection, however you must practice more in order to understand more.
394394

en/02.7.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ goroutines and concurrency are built into the core design of Go. They're similar
88

99
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*** ).
1010
```Go
11-
go hello(a, b, c)
11+
go hello(a, b, c)
1212
```
1313
Let's see an example.
1414
```Go
@@ -56,14 +56,14 @@ Before Go 1.5,The scheduler only uses one thread to run all goroutines, which me
5656

5757
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`.
5858
```Go
59-
ci := make(chan int)
60-
cs := make(chan string)
61-
cf := make(chan interface{})
59+
ci := make(chan int)
60+
cs := make(chan string)
61+
cf := make(chan interface{})
6262
```
6363
channel uses the operator `<-` to send or receive data.
6464
```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
6767
```
6868
Let's see more examples.
6969
```Go
@@ -97,10 +97,10 @@ Sending and receiving data in channels blocks by default, so it's much easier to
9797

9898
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.
9999
```Go
100-
ch := make(chan type, n)
100+
ch := make(chan type, n)
101101

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)
104104
```
105105
You can try the following code on your computer and change some values.
106106
```Go

0 commit comments

Comments
 (0)