Format and remove en/02.x.md spaces

This commit is contained in:
vCaesar
2017-07-01 00:02:17 +08:00
parent 2859b37818
commit c93ed673b2
3 changed files with 37 additions and 37 deletions

View File

@@ -33,15 +33,15 @@ There are three more ways to define a struct.
- Assign initial values by order
```Go
P := person{"Tom", 25}
P := person{"Tom", 25}
```
- Use the format `field:value` to initialize the struct without order
```Go
P := person{age:24, name:"Bob"}
P := person{age:24, name:"Bob"}
```
- Define an anonymous struct, then initialize it
```Go
P := struct{name string; age int}{"Amy",18}
P := struct{name string; age int}{"Amy",18}
```
Let's see a complete example.
```Go
@@ -141,8 +141,8 @@ Figure 2.7 Embedding in Student and Human
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!
```Go
mark.Human = Human{"Marcus", 55, 220}
mark.Human.age -= 1
mark.Human = Human{"Marcus", 55, 220}
mark.Human.age -= 1
```
All the types in Go can be used as embedded fields.
```Go

View File

@@ -175,13 +175,13 @@ An interface is a set of abstract methods, and can be implemented by non-interfa
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.
```Go
// define a as empty interface
var a interface{}
var i int = 5
s := "Hello world"
// a can store value of any type
a = i
a = s
// define a as empty interface
var a interface{}
var i int = 5
s := "Hello world"
// a can store value of any type
a = i
a = s
```
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.
@@ -223,8 +223,8 @@ func main() {
```
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.
```Go
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
fmt.Println("The biggest one is", boxes.BiggestsColor())
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
fmt.Println("The biggest one is", boxes.BiggestsColor())
```
Attention: If the type implemented the interface `error`, fmt will call `Error()`, so you don't have to implement Stringer at this point.
@@ -366,29 +366,29 @@ Reflection in Go is used for determining information at runtime. We use the `ref
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).
```Go
t := reflect.TypeOf(i) // get meta-data in type i, and use t to get all elements
v := reflect.ValueOf(i) // get actual value in type i, and use v to change its value
t := reflect.TypeOf(i) // get meta-data in type i, and use t to get all elements
v := reflect.ValueOf(i) // get actual value in type i, and use v to change its value
```
After that, we can convert the reflected types to get the values that we need.
```Go
var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.Type())
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
fmt.Println("value:", v.Float())
var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.Type())
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
fmt.Println("value:", v.Float())
```
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.
```Go
var x float64 = 3.4
v := reflect.ValueOf(x)
v.SetFloat(7.1)
var x float64 = 3.4
v := reflect.ValueOf(x)
v.SetFloat(7.1)
```
Instead, we must use the following code to change the values from reflect types.
```Go
var x float64 = 3.4
p := reflect.ValueOf(&x)
v := p.Elem()
v.SetFloat(7.1)
var x float64 = 3.4
p := reflect.ValueOf(&x)
v := p.Elem()
v.SetFloat(7.1)
```
We have just discussed the basics of reflection, however you must practice more in order to understand more.

View File

@@ -8,7 +8,7 @@ goroutines and concurrency are built into the core design of Go. They're similar
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*** ).
```Go
go hello(a, b, c)
go hello(a, b, c)
```
Let's see an example.
```Go
@@ -56,14 +56,14 @@ Before Go 1.5,The scheduler only uses one thread to run all goroutines, which me
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`.
```Go
ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{})
ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{})
```
channel uses the operator `<-` to send or receive data.
```Go
ch <- v // send v to channel ch.
v := <-ch // receive data from ch, and assign to v
ch <- v // send v to channel ch.
v := <-ch // receive data from ch, and assign to v
```
Let's see more examples.
```Go
@@ -97,10 +97,10 @@ Sending and receiving data in channels blocks by default, so it's much easier to
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.
```Go
ch := make(chan type, n)
ch := make(chan type, n)
n == 0 ! non-bufferblock
n > 0 ! buffernon-block until n elements in the channel
n == 0 ! non-bufferblock
n > 0 ! buffernon-block until n elements in the channel
```
You can try the following code on your computer and change some values.
```Go