git » gofer » commit d91ca41

Minor code cleanups post Go 1.24

author Alberto Bertogli
2025-11-30 11:08:42 UTC
committer Alberto Bertogli
2025-11-30 11:19:34 UTC
parent a52cacaf956e19595208114384f4680f4d3d615b

Minor code cleanups post Go 1.24

As we now have a minimum Go version of 1.23, we can remove some
workarounds (like for loop scoping), stop using now-deprecated
functions, and also fix up some Printf errors detected by go vet
(luckily just in tests).

This is purely for cleanup and readability purposes.

config/config.go +2 -2
gofer.go +0 -6
ipratelimit/ipratelimit_test.go +2 -2
server/auth.go +2 -2
test/util/exp/exp.go +1 -1
util/util.go +1 -4

diff --git a/config/config.go b/config/config.go
index 544fa29..e4f58f5 100644
--- a/config/config.go
+++ b/config/config.go
@@ -3,8 +3,8 @@ package config
 
 import (
 	"fmt"
-	"io/ioutil"
 	"net/url"
+	"os"
 	"regexp"
 	"strconv"
 	"strings"
@@ -216,7 +216,7 @@ func nTrue(bs ...bool) int {
 }
 
 func Load(filename string) (*Config, error) {
-	contents, err := ioutil.ReadFile(filename)
+	contents, err := os.ReadFile(filename)
 	if err != nil {
 		return nil, fmt.Errorf("error reading config file: %v", err)
 	}
diff --git a/gofer.go b/gofer.go
index 954384c..a390e8a 100644
--- a/gofer.go
+++ b/gofer.go
@@ -68,24 +68,18 @@ func main() {
 	servers := []runnerFunc{}
 
 	for addr, https := range conf.HTTPS {
-		addr := addr
-		https := https
 		servers = append(servers, func() error {
 			return server.HTTPS(addr, https)
 		})
 	}
 
 	for addr, http := range conf.HTTP {
-		addr := addr
-		http := http
 		servers = append(servers, func() error {
 			return server.HTTP(addr, http)
 		})
 	}
 
 	for addr, raw := range conf.Raw {
-		addr := addr
-		raw := raw
 		servers = append(servers, func() error {
 			return server.Raw(addr, raw)
 		})
diff --git a/ipratelimit/ipratelimit_test.go b/ipratelimit/ipratelimit_test.go
index 3429211..4f85c36 100644
--- a/ipratelimit/ipratelimit_test.go
+++ b/ipratelimit/ipratelimit_test.go
@@ -353,14 +353,14 @@ func TestDebugString(t *testing.T) {
 	l := NewLimiter(1, time.Second, 3)
 	l.Allow(net.IPv4(1, 1, 1, 1))
 	l.Allow(net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888"))
-	t.Logf(l.DebugString())
+	t.Log(l.DebugString())
 }
 
 func TestDebugHTML(t *testing.T) {
 	l := NewLimiter(1, time.Second, 3)
 	l.Allow(net.IPv4(1, 1, 1, 1))
 	l.Allow(net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888"))
-	t.Logf(l.DebugHTML())
+	t.Log(l.DebugHTML())
 }
 
 func BenchmarkDifferentIPv4_256(b *testing.B) {
diff --git a/server/auth.go b/server/auth.go
index 0547b72..0991d91 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -3,9 +3,9 @@ package server
 import (
 	"crypto/sha256"
 	"encoding/hex"
-	"io/ioutil"
 	"math/rand"
 	"net/http"
+	"os"
 	"time"
 
 	"blitiri.com.ar/go/gofer/trace"
@@ -82,7 +82,7 @@ type AuthDB struct {
 }
 
 func LoadAuthFile(path string) (*AuthDB, error) {
-	buf, err := ioutil.ReadFile(path)
+	buf, err := os.ReadFile(path)
 	if err != nil {
 		return nil, err
 	}
diff --git a/test/util/exp/exp.go b/test/util/exp/exp.go
index 5f876a4..b92072c 100644
--- a/test/util/exp/exp.go
+++ b/test/util/exp/exp.go
@@ -178,7 +178,7 @@ func mkTransport(caCert string, forceLocalhost bool) *http.Transport {
 		return nil
 	}
 
-	certs, err := ioutil.ReadFile(caCert)
+	certs, err := os.ReadFile(caCert)
 	if err != nil {
 		fatalf("error reading CA file %q: %v", caCert, err)
 	}
diff --git a/util/util.go b/util/util.go
index 8bd74b6..50b29c8 100644
--- a/util/util.go
+++ b/util/util.go
@@ -6,7 +6,6 @@ import (
 	"crypto/tls"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"sync/atomic"
@@ -124,7 +123,7 @@ func cachePath(confDir string) string {
 func LoadCertsFromDir(certDir string) (*tls.Config, error) {
 	tlsConfig := &tls.Config{}
 
-	infos, err := ioutil.ReadDir(certDir)
+	infos, err := os.ReadDir(certDir)
 	if err != nil {
 		return nil, fmt.Errorf("ReadDir(%q): %v", certDir, err)
 	}
@@ -157,8 +156,6 @@ func LoadCertsFromDir(certDir string) (*tls.Config, error) {
 		return nil, fmt.Errorf("no certificates found in %q", certDir)
 	}
 
-	tlsConfig.BuildNameToCertificate()
-
 	return tlsConfig, nil
 }