You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/06/18 12:25:15 UTC
[incubator-opendal] 01/01: feat: Add golang binding for OpenDAL
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch go-binding
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit ec1441bef10fb114ce186b1ada1e4efe2e550156
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sun Jun 18 20:25:01 2023 +0800
feat: Add golang binding for OpenDAL
Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
bindings/go/README.md | 17 +++++++++++++++++
bindings/go/go.mod | 7 +++++++
bindings/go/go.sum | 4 ++++
bindings/go/init.go | 21 +++++++++++++++++++++
bindings/go/init_linux.go | 9 +++++++++
bindings/go/opendal.go | 16 ++++++++++++++++
bindings/go/opendal_test.go | 8 ++++++++
7 files changed, 82 insertions(+)
diff --git a/bindings/go/README.md b/bindings/go/README.md
new file mode 100644
index 000000000..c747a16a0
--- /dev/null
+++ b/bindings/go/README.md
@@ -0,0 +1,17 @@
+# OpenDAL Golang Binding
+
+Build C binding first.
+
+cd `bindings/c`
+
+```shell
+cargo build
+```
+
+and than cd `bindings/go`
+
+```shell
+CGO_ENABLED=0 LD_LIBRARY_PATH=../../target/debug go test -v
+```
+
+We can call it without CGO (only work under linux).
diff --git a/bindings/go/go.mod b/bindings/go/go.mod
new file mode 100644
index 000000000..5da1c4637
--- /dev/null
+++ b/bindings/go/go.mod
@@ -0,0 +1,7 @@
+module opendal.apache.org/go
+
+go 1.20
+
+require github.com/ebitengine/purego v0.4.0-alpha.4
+
+require golang.org/x/sys v0.7.0 // indirect
diff --git a/bindings/go/go.sum b/bindings/go/go.sum
new file mode 100644
index 000000000..1ce866441
--- /dev/null
+++ b/bindings/go/go.sum
@@ -0,0 +1,4 @@
+github.com/ebitengine/purego v0.4.0-alpha.4 h1:Y7yIV06Yo5M2BAdD7EVPhfp6LZ0tEcQo5770OhYUVes=
+github.com/ebitengine/purego v0.4.0-alpha.4/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
+golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
+golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
diff --git a/bindings/go/init.go b/bindings/go/init.go
new file mode 100644
index 000000000..f61e6a3a5
--- /dev/null
+++ b/bindings/go/init.go
@@ -0,0 +1,21 @@
+package opendal
+
+import (
+ "github.com/ebitengine/purego"
+)
+
+var OPENDAL_LIB uintptr
+
+var opendal_operator_options_new func() uintptr
+var opendal_operator_new func(uintptr) uintptr
+
+func init() {
+ var err error
+ OPENDAL_LIB, err = openLibrary()
+ if err != nil {
+ panic(err)
+ }
+
+ purego.RegisterLibFunc(&opendal_operator_options_new, OPENDAL_LIB, "opendal_operator_options_new")
+ purego.RegisterLibFunc(&opendal_operator_new, OPENDAL_LIB, "opendal_operator_new")
+}
diff --git a/bindings/go/init_linux.go b/bindings/go/init_linux.go
new file mode 100644
index 000000000..51c3a8ef3
--- /dev/null
+++ b/bindings/go/init_linux.go
@@ -0,0 +1,9 @@
+//go:build linux
+
+package opendal
+
+import "github.com/ebitengine/purego"
+
+func openLibrary() (uintptr, error) {
+ return purego.Dlopen("libopendal_c.so", purego.RTLD_NOW|purego.RTLD_GLOBAL)
+}
diff --git a/bindings/go/opendal.go b/bindings/go/opendal.go
new file mode 100644
index 000000000..e0c29c8b3
--- /dev/null
+++ b/bindings/go/opendal.go
@@ -0,0 +1,16 @@
+package opendal
+
+import "unsafe"
+
+type Operator struct {
+ op uintptr
+}
+
+func NewOperator(scheme string) *Operator {
+ trick := append([]byte(scheme), 0)
+ op := opendal_operator_new(uintptr(unsafe.Pointer(&trick[0])))
+
+ return &Operator{
+ op: op,
+ }
+}
diff --git a/bindings/go/opendal_test.go b/bindings/go/opendal_test.go
new file mode 100644
index 000000000..a89d745bc
--- /dev/null
+++ b/bindings/go/opendal_test.go
@@ -0,0 +1,8 @@
+package opendal
+
+import "testing"
+
+func TestNew(t *testing.T) {
+ op := NewOperator("memory")
+ t.Logf("%v", op)
+}