Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions bindings/go/README.md
Original file line number Diff line number Diff line change
@@ -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).
7 changes: 7 additions & 0 deletions bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module opendal.apache.org/go

go 1.20
Comment thread
Xuanwo marked this conversation as resolved.

require github.com/ebitengine/purego v0.4.0-alpha.4
Comment thread
Xuanwo marked this conversation as resolved.

require golang.org/x/sys v0.7.0 // indirect
4 changes: 4 additions & 0 deletions bindings/go/go.sum
Original file line number Diff line number Diff line change
@@ -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=
21 changes: 21 additions & 0 deletions bindings/go/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package opendal

import (
"github.com/ebitengine/purego"
)

var OPENDAL_LIB uintptr

var opendal_operator_options_new func() uintptr
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Ji-Xinyou, I will meet segmentation violation if I'm trying to pass the returning value of opendal_operator_options_new into opendal_operator_new. Maybe it's related to #2462?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to fix that now

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")
}
9 changes: 9 additions & 0 deletions bindings/go/init_linux.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions bindings/go/opendal.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
8 changes: 8 additions & 0 deletions bindings/go/opendal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package opendal

import "testing"

func TestNew(t *testing.T) {
op := NewOperator("memory")
t.Logf("%v", op)
}