提交 58c93664 编写于 作者: A Anchor

Grammar and typo fixes for 02.6.md

上级 8e3a6026
......@@ -6,15 +6,15 @@ One of the subtlest design features in Go are interfaces. After reading this sec
### What is an interface
In short, an interface is a set of methods, that we use to define a set of actions.
In short, an interface is a set of methods that we use to define a set of actions.
Like the examples in previous sections, both Student and Employee can `SayHi()`, but they don't do the same thing.
Let's do more work, we add one more method `Sing()` to them, also add `BorrowMoney()` to Student and `SpendSalary()` to Employee.
Let's do some more work. We'll add one more method `Sing()` to them, along with the `BorrowMoney()` method to Student and the `SpendSalary()` method to Employee.
Now Student has three methods called `SayHi()`, `Sing()`, `BorrowMoney()`, and Employee has `SayHi()`, `Sing()` and `SpendSalary()`.
Now, Student has three methods called `SayHi()`, `Sing()` and `BorrowMoney()`, and Employee has `SayHi()`, `Sing()` and `SpendSalary()`.
This combination of methods is called an interface, and is implemented by Student and Employee. So Student and Employee implement the interface: `SayHi()`, `Sing()`. At the same time, Employee doesn't implement the interface: `SayHi()`, `Sing()`, `BorrowMoney()`, and Student doesn't implement the interface: `SayHi()`, `Sing()`, `SpendSalary()`. This is because Employee doesn't have the method `BorrowMoney()` and Student doesn't have the method `SpendSalary()`.
This combination of methods is called an interface and is implemented by both Student and Employee. So, Student and Employee implement the interface: `SayHi()` and `Sing()`. At the same time, Employee doesn't implement the interface: `SayHi()`, `Sing()`, `BorrowMoney()`, and Student doesn't implement the interface: `SayHi()`, `Sing()`, `SpendSalary()`. This is because Employee doesn't have the method `BorrowMoney()` and Student doesn't have the method `SpendSalary()`.
### Type of Interface
......@@ -83,15 +83,15 @@ An interface defines a set of methods, so if a type implements all the methods w
SpendSalary(amount float32)
}
We know that an interface can be implemented by any type, and one type can implement many interfaces at the same time.
We know that an interface can be implemented by any type, and one type can implement many interfaces simultaneously.
Note that any type implements the empty interface `interface{}` because it doesn't have any methods and all types have zero methods as default.
Note that any type implements the empty interface `interface{}` because it doesn't have any methods and all types have zero methods by default.
### Value of interface
So what kind of values can be put in the interface? If we define a variable as a type interface, any type that implements the interface can assigned to this variable.
Like the above example, if we define a variable m as interface Men, then any one of Student, Human or Employee can be assigned to m. So we could have a slice of Men, and any type that implements interface Men can assign to this slice. Be aware however that the slice of interface doesn't have the same behavior as a slice of other types.
Like the above example, if we define a variable "m" as interface Men, then any one of Student, Human or Employee can be assigned to "m". So we could have a slice of Men, and any type that implements interface Men can assign to this slice. Be aware however that the slice of interface doesn't have the same behavior as a slice of other types.
package main
......@@ -171,7 +171,7 @@ An interface is a set of abstract methods, and can be implemented by non-interfa
### Empty interface
An empty interface is an interface that doesn't contain any methods, so all types implemented an empty interface. It's very useful when we want to store all types at some point, and is similar to void* in C.
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.
// define a as empty interface
var a interface{}
......@@ -185,9 +185,9 @@ If a function uses an empty interface as its argument type, it can accept any ty
### Method arguments of an interface
Any variable that can be used in an interface, so we can think about how can we use this feature to pass any type of variable to the function.
Any variable can be used in an interface. So how can we use this feature to pass any type of variable to a function?
For example, we use fmt.Println a lot, but have you ever noticed that it accepts any type of arguments? Looking at the open source code of fmt, we see the following definition.
For example we use fmt.Println a lot, but have you ever noticed that it can accept any type of argument? Looking at the open source code of fmt, we see the following definition.
type Stringer interface {
String() string
......@@ -228,11 +228,11 @@ Attention: If the type implemented the interface `error`, fmt will call `error()
### Type of variable in an interface
If a variable is the type that implements an interface, we know that any other type that implements the same interface can be assigned to this variable. The question is how can we know the specific type stored in the interface. There are two ways that I'm going to tell you.
If a variable is the type that implements an interface, we know that any other type that implements the same interface can be assigned to this variable. The question is how can we know the specific type stored in the interface. There are two ways which I will show you.
- Assertion of Comma-ok pattern
Go has the syntax `value, ok := element.(T)`. This checks to see if the variable is the type that we expect, where the value is the value of the variable, ok is a variable of boolean type, element is the interface variable and the T is the type of assertion.
Go has the syntax `value, ok := element.(T)`. This checks to see if the variable is the type that we expect, where "value" is the value of the variable, "ok" is a variable of boolean type, "element" is the interface variable and the T is the type of assertion.
If the element is the type that we expect, ok will be true, false otherwise.
......@@ -322,13 +322,13 @@ Let's use `switch` to rewrite the above example.
}
One thing you should remember is that `element.(type)` cannot be used outside of `switch` body, which means in that case you have you use pattern `comma-ok`.
One thing you should remember is that `element.(type)` cannot be used outside of the `switch` body, which means in that case you have to use the `comma-ok` pattern .
### Embedded interfaces
The most beautiful thing is that Go has a lot of built-in logic syntax, such as anonymous fields in struct. Not suprisingly, we can use interfaces as anonymous fields as well, but we call them `Embedded interfaces`. Here, we follows the same rules as anonymous fields. More specifically, if an interface has another interface as the embedded interface, it will have all the methods that the embedded interface has.
The most beautiful thing is that Go has a lot of built-in logic syntax, such as anonymous fields in struct. Not suprisingly, we can use interfaces as anonymous fields as well, but we call them `Embedded interfaces`. Here, we follow the same rules as anonymous fields. More specifically, if an interface has another interface embedded within it, it will have as if it has all the methods that the embedded interface has.
We can see source file in `container/heap` has one definition as follows.
We can see that the source file in `container/heap` has the following definition:
type Interface interface {
sort.Interface // embedded sort.Interface
......@@ -336,7 +336,7 @@ We can see source file in `container/heap` has one definition as follows.
Pop() interface{} //a Pop elements that pops elements from the heap
}
We see that `sort.Interface` is an embedded interface, so the above Interface has three methods that in `sort.Interface` implicitly.
We see that `sort.Interface` is an embedded interface, so the above Interface has the three methods contained within the `sort.Interface` implicitly.
type Interface interface {
// Len is the number of elements in the collection.
......@@ -360,12 +360,12 @@ Another example is the `io.ReadWriter` in package `io`.
Reflection in Go is used for determining information at runtime. We use the `reflect` package, and this official [article](http://golang.org/doc/articles/laws_of_reflection.html) explains how reflect works in Go.
There are three steps to use reflect. First, we need to convert an interface to reflect types (reflect.Type or reflect.Value, this depends on the situation).
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).
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 convert reflect types to get values that we need.
After that, we can convert the reflect types to get values that we need.
var x float64 = 3.4
v := reflect.ValueOf(x)
......@@ -373,20 +373,20 @@ After that, we convert reflect types to get values that we need.
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
fmt.Println("value:", v.Float())
Finally, if we want to change a value that is from reflect 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.
Finally, if we want to change values from reflect 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.
var x float64 = 3.4
v := reflect.ValueOf(x)
v.SetFloat(7.1)
Instead, we must use the following code to change value from reflect types.
Instead, we must use the following code to change the values from reflect types.
var x float64 = 3.4
p := reflect.ValueOf(&x)
v := p.Elem()
v.SetFloat(7.1)
I just talked about basic knowledge about reflection, you must pratice more to understand more.
We have just discussed the basics of reflection, however you must practice more in order to understand more.
## Links
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册