-
-
Save up1/320c8f68fb611dcf9bc6eebe7d9a33fd to your computer and use it in GitHub Desktop.
Go 1.24 rc2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"testing" | |
) | |
func BenchmarkOld(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
try() | |
} | |
} | |
func BenchmarkOld2(b *testing.B) { | |
for range b.N { | |
try() | |
} | |
} | |
func BenchmarkNew(b *testing.B) { | |
for b.Loop() { | |
try() | |
} | |
} | |
// Run | |
$go1.24rc2 test -bench=. | |
goos: darwin | |
goarch: arm64 | |
pkg: demo | |
cpu: Apple M2 Max | |
BenchmarkOld-12 1000000000 0.2963 ns/op | |
BenchmarkOld2-12 1000000000 0.2917 ns/op | |
BenchmarkNew-12 593273757 2.006 ns/op | |
PASS | |
ok demo 2.435s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
type Resource struct { | |
Id int | |
Name string | |
} | |
func release(time time.Time) { | |
fmt.Println("Release resource at", time) | |
} | |
// Run | |
$go1.24rc2 run clean.go | |
Release resource at 2025-02-05 22:40:32.22332 +0700 +07 m=+0.000141001 | |
func makeResource() *Resource { | |
r := &Resource{} | |
// Register the resource for cleanup | |
runtime.AddCleanup(r, release, time.Now()) | |
return r | |
} | |
func main() { | |
// Create a resource | |
makeResource() | |
// Force GC to run | |
runtime.GC() | |
// Wait for the cleanup to run | |
time.Sleep(100 * time.Millisecond) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"testing/synctest" | |
"time" | |
) | |
func TestMyConcurrentCode(t *testing.T) { | |
// Run the test with a timeout of 5 seconds | |
synctest.Run(func() { | |
go func() { | |
time.Sleep(5 * time.Second) | |
}() | |
// Wait for the goroutine to finish | |
synctest.Wait() | |
}) | |
} | |
// Run | |
$GOEXPERIMENT=synctest go1.24rc2 test ./... -v | |
=== RUN TestMyConcurrentCode | |
--- PASS: TestMyConcurrentCode (0.00s) | |
PASS | |
ok demo 0.192s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log/slog" | |
) | |
func main() { | |
// Discard log output with slog | |
// Old way | |
log1 := slog.New( | |
slog.NewTextHandler(io.Discard, nil), | |
) | |
log1.Info("This will not be printed") | |
// New way | |
log2 := slog.New(slog.DiscardHandler) | |
log2.Info("This will not be printed") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment