From f5f0d12b697e71e67a403791c1b93ffdf61e272a Mon Sep 17 00:00:00 2001 From: xegineering Date: Fri, 29 Nov 2024 10:16:57 +0100 Subject: Refactor streamContext() This avoids an unnecessary additional for loop. --- soundbox/streaming.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/soundbox/streaming.go b/soundbox/streaming.go index 357fa30..3eb237d 100644 --- a/soundbox/streaming.go +++ b/soundbox/streaming.go @@ -13,24 +13,23 @@ const writeTimeout = 1 * time.Second func streamContext(ctx context.Context, r io.Reader, targets []net.HardwareAddr) error { conns := make([]net.Conn, 0) + writers := make([]io.Writer, 0) + for _, target := range targets { conn, err := dialContext(ctx, target) if err != nil { return err } conns = append(conns, conn) + writers = append(writers, conn) } + defer func() { for _, conn := range conns { conn.Close() } }() - writers := make([]io.Writer, 0) - for _, conn := range conns { - writers = append(writers, conn) - } - mw := io.MultiWriter(writers...) buf := make([]byte, bufferSize) -- cgit v1.2.3-70-g09d2