Merge pull request #2 from kinghrothgar/clarifyParseSection

Clarified parse section of program-structure-and-composability
This commit is contained in:
Brian Picciano 2019-09-10 12:00:24 -06:00 committed by GitHub
commit 24656a8a75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 cant use the redis connection until that happens, but that get populated? It cant happen inside `redis.NewConn` because there might be
cant 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, lets look at a simple program that wants to set up two
problem, lets 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