Dear all,

I am writing a program which processes data in stages.  To structure the 
code cleanly, I would like implement each stage as an individual function 
and use channels to pass data between stages.

My questions:
- How to structure the code?  Is my approach described below reasonable?
- How to deal with errors?

Here is a sketch of I currently have (runnable version 
at https://go.dev/play/p/7Rrq-OLARl_R ):

func getInputs() <-chan t1 {
    c := make(chan t1)
    go func() {
        for ... {
            ...
            c <- x
        }
        close(c)
    }()
    return c
}

func process(in <-chan t1) <-chan t2 {
    c := make(chan t2)
    go func() {
    for i := range in {
        x, err := ...
        _ = err // what to do with the error?
        c <- x
    }
    close(c)
    }()
    return c
}

func main() {
    c1 := getInputs()
    c2 := process(c1)
    summarise(c2)
}

There are some things I like about this approach:

   - the main function looks nice and tidy
   - the stages of work are cleanly separated

But there are also things I don't like:

   - I can't see an obvious way to handle errors in the stages.  In my case 
   the first stage is locating input files on the file system, and there may 
   be read errors etc.  The second stage processes the files, and files may 
   have invalid contents.
   - The code for the individual stages looks a bit strange with the double 
   indentation from the outer func(){} and the go func() {}().

So my questions are:

   - What would be a good way to incorporate error handling into my code?  
   I assume this will require one or more additional channels, just for 
   errors.  Or is there a better way?
   - Is there a better way to structure my code?

Many thanks,
Jochen

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/cd4e086c-4f1c-4a48-9162-c46ab45df66fn%40googlegroups.com.

Reply via email to