all: run go fix ./... Change-Id: I66713fa220534c7d26ea2c7793d5e66a905706f4 Reviewed-on: https://go-review.googlesource.com/c/playground/+/802182 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cache.go b/cache.go index dca1d23..58d0852 100644 --- a/cache.go +++ b/cache.go
@@ -14,9 +14,9 @@ // responseCache is a common interface for cache implementations. type responseCache interface { // Set sets the value for a key. - Set(key string, v interface{}) error + Set(key string, v any) error // Get sets v to the value stored for a key. - Get(key string, v interface{}) error + Get(key string, v any) error } // gobCache stores and retrieves values using a memcache client using the gob @@ -30,7 +30,7 @@ return &gobCache{memcache.New(addr)} } -func (c *gobCache) Set(key string, v interface{}) error { +func (c *gobCache) Set(key string, v any) error { if c == nil { return nil } @@ -41,7 +41,7 @@ return c.client.Set(&memcache.Item{Key: key, Value: buf.Bytes()}) } -func (c *gobCache) Get(key string, v interface{}) error { +func (c *gobCache) Get(key string, v any) error { if c == nil { return memcache.ErrCacheMiss }
diff --git a/examples.go b/examples.go index d834d1f..bf4bb28 100644 --- a/examples.go +++ b/examples.go
@@ -66,12 +66,12 @@ // Read examples ending in .txt, skipping those ending in .gotip.txt if // gotip is not set. prefix := "" // if non-empty, this is a relevant example file - if strings.HasSuffix(name, ".gotip.txt") { + if p, ok := strings.CutSuffix(name, ".gotip.txt"); ok { if gotip { - prefix = strings.TrimSuffix(name, ".gotip.txt") + prefix = p } - } else if strings.HasSuffix(name, ".txt") { - prefix = strings.TrimSuffix(name, ".txt") + } else if p, ok := strings.CutSuffix(name, ".txt"); ok { + prefix = p } if prefix == "" {
diff --git a/internal/internal_test.go b/internal/internal_test.go index 337bbbc..25ea9ca 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go
@@ -13,7 +13,7 @@ func TestPeriodicallyDo(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) didWork := make(chan time.Time, 2) - done := make(chan interface{}) + done := make(chan any) go func() { PeriodicallyDo(ctx, 100*time.Millisecond, func(ctx context.Context, t time.Time) { select {
diff --git a/logger.go b/logger.go index 2d36289..3b0afce 100644 --- a/logger.go +++ b/logger.go
@@ -10,9 +10,9 @@ ) type logger interface { - Printf(format string, args ...interface{}) - Errorf(format string, args ...interface{}) - Fatalf(format string, args ...interface{}) + Printf(format string, args ...any) + Errorf(format string, args ...any) + Fatalf(format string, args ...any) } // stdLogger implements the logger interface using the log package. @@ -30,14 +30,14 @@ } } -func (l *stdLogger) Printf(format string, args ...interface{}) { +func (l *stdLogger) Printf(format string, args ...any) { l.stdout.Printf(format, args...) } -func (l *stdLogger) Errorf(format string, args ...interface{}) { +func (l *stdLogger) Errorf(format string, args ...any) { l.stderr.Printf(format, args...) } -func (l *stdLogger) Fatalf(format string, args ...interface{}) { +func (l *stdLogger) Fatalf(format string, args ...any) { l.stderr.Fatalf(format, args...) }
diff --git a/play.go b/play.go index 41e7058..ad83591 100644 --- a/play.go +++ b/play.go
@@ -85,10 +85,7 @@ ) for _, e := range events { - delay := e.time.Sub(now) - if delay < 0 { - delay = 0 - } + delay := max(e.time.Sub(now), 0) out = append(out, Event{ Message: string(sanitize(e.msg)), Kind: e.kind, @@ -165,10 +162,7 @@ // Slurp output. // Truncated output is OK (probably caused by sandbox limits). - end := i + n - if end > len(output) { - end = len(output) - } + end := min(i+n, len(output)) add(t, output[i:end]) i += n }
diff --git a/sandbox/sandbox.go b/sandbox/sandbox.go index f266caa..3466158 100644 --- a/sandbox/sandbox.go +++ b/sandbox/sandbox.go
@@ -545,7 +545,7 @@ t0 := time.Now() tlast := t0 var logmu sync.Mutex - logf := func(format string, args ...interface{}) { + logf := func(format string, args ...any) { if !*dev { return } @@ -746,9 +746,9 @@ // cleanStderr removes spam stderr lines from the beginning of x // and returns a slice of x. func cleanStderr(x []byte) []byte { - i := bytes.Index(x, containedStderrHeader) - if i == -1 { + _, stderr, ok := bytes.Cut(x, containedStderrHeader) + if !ok { return x } - return x[i+len(containedStderrHeader):] + return stderr }
diff --git a/sandbox_test.go b/sandbox_test.go index 99f1f81..8a83b17 100644 --- a/sandbox_test.go +++ b/sandbox_test.go
@@ -51,8 +51,8 @@ t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out) isTestFunction := map[string]bool{} - lines := strings.Split(string(out), "\n") - for _, line := range lines { + lines := strings.SplitSeq(string(out), "\n") + for line := range lines { // We want Test/Benchmark/Example/Fuzz functions. // Reject extraneous output such as "ok ...". if line == "" || !strings.Contains("TBEF", line[:1]) {
diff --git a/server.go b/server.go index 1ecb134..b80362d 100644 --- a/server.go +++ b/server.go
@@ -97,7 +97,7 @@ // writeJSONResponse JSON-encodes resp and writes to w with the given HTTP // status. -func (s *server) writeJSONResponse(w http.ResponseWriter, resp interface{}, status int) { +func (s *server) writeJSONResponse(w http.ResponseWriter, resp any, status int) { w.Header().Set("Content-Type", "application/json") var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(resp); err != nil {
diff --git a/server_test.go b/server_test.go index 6a5362c..3d71058 100644 --- a/server_test.go +++ b/server_test.go
@@ -26,13 +26,13 @@ t *testing.T } -func (l testLogger) Printf(format string, args ...interface{}) { +func (l testLogger) Printf(format string, args ...any) { l.t.Logf(format, args...) } -func (l testLogger) Errorf(format string, args ...interface{}) { +func (l testLogger) Errorf(format string, args ...any) { l.t.Errorf(format, args...) } -func (l testLogger) Fatalf(format string, args ...interface{}) { +func (l testLogger) Fatalf(format string, args ...any) { l.t.Fatalf(format, args...) } @@ -361,7 +361,7 @@ // Set implements the responseCache interface. // Set stores a *response in the cache. It panics for other types to ensure test failure. -func (i *inMemCache) Set(key string, v interface{}) error { +func (i *inMemCache) Set(key string, v any) error { i.l.Lock() defer i.l.Unlock() if i.m == nil { @@ -374,7 +374,7 @@ // Get implements the responseCache interface. // Get fetches a *response from the cache, or returns a memcache.ErrCacheMiss. // It panics for other types to ensure test failure. -func (i *inMemCache) Get(key string, v interface{}) error { +func (i *inMemCache) Get(key string, v any) error { i.l.Lock() defer i.l.Unlock() target := v.(*response)
diff --git a/tests.go b/tests.go index b0911a1..18cf53b 100644 --- a/tests.go +++ b/tests.go
@@ -226,7 +226,7 @@ return nil } have := map[string]bool{} - for _, f := range strings.Split(got, "\n") { + for f := range strings.SplitSeq(got, "\n") { have[f] = true } for _, expect := range []string{