Format and remove en/02.x.md spaces
This commit is contained in:
46
en/02.6.md
46
en/02.6.md
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user