xieyuschen / quic-example

Explore the http3 by quic with examples
Apache License 2.0
14 stars 2 forks source link

Copy a value and use it in range for #4

Open xieyuschen opened 2 years ago

xieyuschen commented 2 years ago

Note to use the value in range for when you want to execute something in another go routine. In the following code snippet, the bs means a set of address. Note the sentence bCap := b which make sure the err = http3.ListenAndServe(bCap, certFile, keyFile, handler) will receive a expected value when it's executing.

for _, b := range bs {
bCap := b
go func() {
    var err error

    if *tcp {
        err = http3.ListenAndServe(bCap, certFile, keyFile, handler)
    } else {

        server := http3.Server{
            Handler:    handler,
            Addr:       bCap,
            QuicConfig: quicConf,
        }

        err = server.ListenAndServeTLS(certFile, keyFile)
    }
    if err != nil {
        fmt.Printf("failed to listen and serve at %s: %s\n", bCap, err)
        os.Exit(-1)
    }
}()
}