From a36f6210c060ba08e1ca2100155a1d466c15101f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 12 Sep 2024 00:20:49 +0900 Subject: [PATCH] all: use Go 1.20 APIs Closes #2746 --- audio/audio.go | 9 +-------- audio/internal/convert/float32_test.go | 5 ++--- internal/glfw/context_windows.go | 3 +-- internal/graphicsdriver/opengl/gl/procaddr_darwin.go | 3 +-- internal/graphicsdriver/opengl/gl/procaddr_linbsd.go | 12 ++++++------ 5 files changed, 11 insertions(+), 21 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 22a9d604a..254735a94 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -188,14 +188,7 @@ func (c *Context) addPlayingPlayer(p *playerImpl) { defer c.m.Unlock() c.playingPlayers[p] = struct{}{} - // (reflect.Type).Comparable() is enough here, as reflect.TypeOf should always return a dynamic (non-interface) type. - // If reflect.TypeOf returned an interface type, this check would be meaningless. - // See these for more details: - // * https://pkg.go.dev/reflect#TypeOf - // * https://pkg.go.dev/reflect#Type.Comparable - // - // (*reflect.Value).Comparable() is more intuitive but this was introduced in Go 1.20. - if !reflect.TypeOf(p.sourceIdent()).Comparable() { + if !reflect.ValueOf(p.sourceIdent()).Comparable() { return } diff --git a/audio/internal/convert/float32_test.go b/audio/internal/convert/float32_test.go index 268459e19..8d48e33fc 100644 --- a/audio/internal/convert/float32_test.go +++ b/audio/internal/convert/float32_test.go @@ -69,15 +69,14 @@ func TestFloat32(t *testing.T) { name = "seek" } t.Run(name, func(t *testing.T) { - // Note that unsafe.SliceData is available as of Go 1.20. var in, out []byte if len(c.In) > 0 { outF32 := make([]float32, len(c.In)) for i := range c.In { outF32[i] = float32(c.In[i]) / (1 << 15) } - in = unsafe.Slice((*byte)(unsafe.Pointer(&c.In[0])), len(c.In)*2) - out = unsafe.Slice((*byte)(unsafe.Pointer(&outF32[0])), len(outF32)*4) + in = unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(c.In))), len(c.In)*2) + out = unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(outF32))), len(outF32)*4) } r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)).(io.ReadSeeker) var got []byte diff --git a/internal/glfw/context_windows.go b/internal/glfw/context_windows.go index 49e0fdc4c..0f8f85c30 100644 --- a/internal/glfw/context_windows.go +++ b/internal/glfw/context_windows.go @@ -616,6 +616,5 @@ func bytePtrToString(p *byte) string { ptr = unsafe.Add(ptr, 1) } - // unsafe.String(p, n) is available as of Go 1.20. - return string(unsafe.Slice(p, n)) + return unsafe.String(p, n) } diff --git a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go index b18959146..e1e25a0a1 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go @@ -38,8 +38,7 @@ func (c *defaultContext) init() error { return nil } - // TODO: Use multiple %w-s as of Go 1.20 - return fmt.Errorf("gl: failed to load: OpenGL.framework: %v, OpenGLES.framework: %v", errGL, errGLES) + return fmt.Errorf("gl: failed to load: OpenGL.framework: %w, OpenGLES.framework: %w", errGL, errGLES) } func (c *defaultContext) getProcAddress(name string) (uintptr, error) { diff --git a/internal/graphicsdriver/opengl/gl/procaddr_linbsd.go b/internal/graphicsdriver/opengl/gl/procaddr_linbsd.go index e7e869ff5..794c9057e 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_linbsd.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_linbsd.go @@ -17,8 +17,8 @@ package gl import ( + "errors" "fmt" - "strings" "github.com/ebitengine/purego" ) @@ -29,8 +29,7 @@ var ( ) func (c *defaultContext) init() error { - // TODO: Use multiple %w-s as of Go 1.20. - var errors []string + var errs []error // Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with OpenGL ES. for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} { @@ -40,7 +39,7 @@ func (c *defaultContext) init() error { c.isES = true return nil } - errors = append(errors, fmt.Sprintf("%s: %v", name, err)) + errs = append(errs, fmt.Errorf("gl: Dlopen failed: name: %s: %w", name, err)) } // Try OpenGL next. @@ -54,10 +53,11 @@ func (c *defaultContext) init() error { libGL = lib return nil } - errors = append(errors, fmt.Sprintf("%s: %v", name, err)) + errs = append(errs, fmt.Errorf("gl: Dlopen failed: name: %s: %w", name, err)) } - return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", ")) + errs = append([]error{fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: ")}, errs...) + return errors.Join(errs...) } func (c *defaultContext) getProcAddress(name string) (uintptr, error) {