Skip to content

Commit

Permalink
Merge pull request quii#96 from gypsydave5/patch-8
Browse files Browse the repository at this point in the history
Typos
  • Loading branch information
quii authored Jul 5, 2018
2 parents 863cb8e + 456a1ee commit 6806abe
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions reflection.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ From [The Go Blog: Reflection](https://blog.golang.org/laws-of-reflection)

## What is `interface` ?

We have enjoyed the type-safety that Go has offered us in terms of functions that work with known types, such as `strings`, `int32` and our own types like `BankAccount`.
We have enjoyed the type-safety that Go has offered us in terms of functions that work with known types, such as `string`, `int` and our own types like `BankAccount`.

This means that we get some documentation for free and the compiler will complain if you try and pass the wrong type to a function.

You may come across scenarios though where you want to write a function where you dont know the type at compile time.
You may come across scenarios though where you want to write a function where you don't know the type at compile time.

Go lets us get around this with the type `interface{}` which you can think of as just _any_ type.

Expand Down Expand Up @@ -61,7 +61,6 @@ func TestWalk(t *testing.T) {
- We use an anonymous `struct` with a `Name` field of type string to go for the simplest "happy" path.
- Finally call `walk` with `x` and the spy and for now just check the length of `got`, we'll be more specific with our assertions once we've got something very basic working.


## Try to run the test

```
Expand Down Expand Up @@ -134,13 +133,13 @@ We need to use reflection to have a look at `x` and try and look at its properti

The [reflect package](https://godoc.org/reflect) has a function `ValueOf` which returns us a `Value` of a given variable. This has ways for us to inspect a value, including its fields which we use on the next line.

We then make some very silly assumptions about the the value passed in
We then make some very optimistic assumptions about the the value passed in
- We look at the first and only field, there may be no fields at all which would cause a panic
- We then call `String()` which returns the underlying value as a string but we know it would be wrong if the field was something other than a string.

## Refactor

Our code is passing for the simple case but we know there's a lot of shortcomings in our code.
Our code is passing for the simple case but we know our code has a lot of shortcomings.

We're going to be writing a number of tests where we pass in different values and checking the array of strings that `fn` was called with.

Expand Down Expand Up @@ -425,7 +424,7 @@ func walk(x interface{}, fn func(input string)) {
}
```

You cant use `NumField` on a pointer `Value`, we need to extract the underlying value before we can do that by using `Elem()`
You can't use `NumField` on a pointer `Value`, we need to extract the underlying value before we can do that by using `Elem()`

## Refactor

Expand Down Expand Up @@ -459,7 +458,7 @@ func getValue(x interface{}) reflect.Value {
```

This actually adds _more_ code but I feel the abstraction level is right
- Get the `reflect.Value` of `x` so i can inspect it, I dont care how.
- Get the `reflect.Value` of `x` so I can inspect it, I don't care how.
- Iterate over the fields, doing whatever needs to be done depending on its type

Next we need to cover slices
Expand Down

0 comments on commit 6806abe

Please sign in to comment.