Merge pull request #2 from kinghrothgar/clarifyParseSection
Clarified parse section of program-structure-and-composability
This commit is contained in:
commit
24656a8a75
@ -376,11 +376,10 @@ Easy-peasy.
|
|||||||
|
|
||||||
Sharp-eyed gophers will notice that there is a key piece missing: When is
|
Sharp-eyed gophers will notice that there is a key piece missing: When is
|
||||||
`flag.Parse`, or its `mcfg` counterpart, called? When does `addrParam` actually
|
`flag.Parse`, or its `mcfg` counterpart, called? When does `addrParam` actually
|
||||||
get populated? You can’t use the redis connection until that happens, but that
|
get populated? It can’t happen inside `redis.NewConn` because there might be
|
||||||
can’t happen inside `redis.NewConn` because there might be other components
|
other components after `redis.NewConn` that want to set up parameters. To
|
||||||
after `redis.NewConn` that want to set up parameters. To illustrate the
|
illustrate the problem, let’s look at a simple program that wants to set up two
|
||||||
problem, let’s look at a simple program that wants to set up two `redis`
|
`redis` components:
|
||||||
components:
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
@ -402,14 +401,18 @@ func main() {
|
|||||||
// before, redis.NewConn can't parse command-line.
|
// before, redis.NewConn can't parse command-line.
|
||||||
barRedis := redis.NewConn(cmpBar, "127.0.0.1:6379")
|
barRedis := redis.NewConn(cmpBar, "127.0.0.1:6379")
|
||||||
|
|
||||||
// If the command-line is parsed here, then how can fooRedis and barRedis
|
// It is only after all components have been instantiated that the
|
||||||
// have been created yet? It's only _after_ this point that `fooRedis` and
|
// command-line arguments can be parsed
|
||||||
// `barRedis` could possibly be usable.
|
|
||||||
mcfg.Parse()
|
mcfg.Parse()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We will solve this problem in the next section.
|
While this solves our argument parsing problem, fooRedis and barRedis are not
|
||||||
|
usable yet because the actual connections have not been made. This is a classic
|
||||||
|
chicken and the egg problem. The func `redis.NewConn` needs to make a connection
|
||||||
|
which it cannot do until _after_ `mcfg.Parse` is called, but `mcfg.Parse` cannot
|
||||||
|
be called until after `redis.NewConn` has returned. We will solve this problem
|
||||||
|
in the next section.
|
||||||
|
|
||||||
### Instantiation vs Initialization
|
### Instantiation vs Initialization
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user