On Sat, Aug 12, 2023 at 12:18 AM Eric Blake <eblake(a)redhat.com> wrote:
Go 1.17 or newer is required to use unsafe.Slice(), which in turn
allows us to write a simpler conversion from a C array to a Go object
during callbacks.
To check if this makes sense, look at
https://repology.org/project/go/versions compared to our list in
ci/manifest.yml, at the time I made this commit:
Alpine 3.15: 1.17.10
AlmaLinux 8: 1.19.10
CentOS Stream 8: 1.20.4
Debian 10: 1.11.6
Debian 11: 1.15.15 (mainline), 1.19.8 (backports)
Debian 12: 1.19.8
Fedoar 36: 1.19.8
FreeBSD Ports: 1.20.7
OpenSUSE Leap 15.3: 1.16.3
OpenSUSE Leap 15.4: 1.18.1
Ubuntu 18.04: 1.18.1
We previously required a minimum of 1.13 for module support, which
means Debian 10 was already not supporting Go bindings. OpenSUSE Leap
15.3 loses support, but is relatively old these days. All other
systems appear unaffected by this bump in requirements, at least if
they can be configured to use developer backports.
Suggested-by: Nir Soffer <nsoffer(a)redhat.com>
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
This replaces
https://listman.redhat.com/archives/libguestfs/2023-August/032227.html
generator/GoLang.ml | 8 ++++----
README.md | 2 +-
golang/configure/go.mod | 4 ++--
golang/configure/test.go | 11 +++++++++++
golang/go.mod | 4 ++--
5 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/generator/GoLang.ml b/generator/GoLang.ml
index 73df5254..55ff1b8a 100644
--- a/generator/GoLang.ml
+++ b/generator/GoLang.ml
@@ -517,10 +517,10 @@ let
func copy_uint32_array(entries *C.uint32_t, count C.size_t) []uint32 {
ret := make([]uint32, int(count))
- // See
https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
- // TODO: Use unsafe.Slice() when we require Go 1.17.
- s := (*[1 << 30]uint32)(unsafe.Pointer(entries))[:count:count]
We have another instance of this pattern in AioBuffer.Slice
- copy(ret, s)
+ s := unsafe.Slice(entries, count)
+ for i, item := range s {
+ ret[i] = uint32(item)
+ }
return ret
}
";
diff --git a/README.md b/README.md
index c7166613..8524038e 100644
--- a/README.md
+++ b/README.md
@@ -105,7 +105,7 @@ ## Building from source
* Python >= 3.3 to build the Python 3 bindings and NBD shell (nbdsh).
* FUSE 3 to build the nbdfuse program.
* Linux >= 6.0 and ublksrv library to build nbdublk program.
-* go and cgo, for compiling the golang bindings and tests.
+* go and cgo >= 1.17, for compiling the golang bindings and tests.
* bash-completion >= 1.99 for tab completion.
Optional, only needed to run the test suite:
diff --git a/golang/configure/go.mod b/golang/configure/go.mod
index ce3e4f39..fcdb28db 100644
--- a/golang/configure/go.mod
+++ b/golang/configure/go.mod
@@ -1,4 +1,4 @@
module
libguestfs.org/configure
-// First version of golang with working module support.
-go 1.13
+// First version of golang with working module support and unsafe.Slice.
"First version of golang with working module support" is not relevant for
1.17,
maybe "For unsafe.Slice"?
+go 1.17
diff --git a/golang/configure/test.go b/golang/configure/test.go
index fe742f2b..a15c9ea3 100644
--- a/golang/configure/test.go
+++ b/golang/configure/test.go
@@ -25,8 +25,19 @@
import (
"fmt"
"runtime"
+ "unsafe"
)
+func check_slice(arr *uint32, cnt int) []uint32 {
+ /* We require unsafe.Slice(), introduced in 1.17 */
+ ret := make([]uint32, cnt)
+ s := unsafe.Slice(arr, cnt)
+ for i, item := range s {
+ ret[i] = uint32(item)
+ }
+ return ret
+}
I'm not sure what is the purpose of this test - requiring the Go version is
good
enough since the code will not compile with an older version. EVen if it
would,
it will not compile without unsafe.Slice so no special check is needed.
+
func main() {
fmt.Println(runtime.Version())
diff --git a/golang/go.mod b/golang/go.mod
index fc772840..1b72e77d 100644
--- a/golang/go.mod
+++ b/golang/go.mod
@@ -1,4 +1,4 @@
module
libguestfs.org/libnbd
-// First version of golang with working module support.
-go 1.13
+// First version of golang with working module support and unsafe.Slice.
+go 1.17
--
2.41.0