Xuanwo commented on code in PR #4886:
URL: https://github.com/apache/opendal/pull/4886#discussion_r1676148068


##########
bindings/go/reader.go:
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package opendal
+
+import (
+       "context"
+       "io"
+       "unsafe"
+
+       "github.com/jupiterrider/ffi"
+       "golang.org/x/sys/unix"
+)
+
+// Read reads the entire contents of the file at the specified path into a 
byte slice.
+//
+// This function is a wrapper around the C-binding function 
`opendal_operator_read`.
+//
+// # Parameters
+//
+//   - path: The path of the file to read.
+//
+// # Returns
+//
+//   - []byte: The contents of the file as a byte slice.
+//   - error: An error if the read operation fails, or nil if successful.
+//
+// # Notes
+//
+//   - This implementation does not support the `read_with` functionality.
+//   - Read allocates a new byte slice internally. For more precise memory 
control
+//     or lazy reading, consider using the Reader() method instead.
+//
+// # Example
+//
+//     func exampleRead(op *opendal.Operator) {
+//             data, err := op.Read("test")
+//             if err != nil {
+//                     log.Fatal(err)
+//             }
+//             fmt.Printf("Read: %s\n", data)
+//     }
+//
+// Note: This example assumes proper error handling and import statements.
+func (op *Operator) Read(path string) ([]byte, error) {
+       read := getFFI[operatorRead](op.ctx, symOperatorRead)
+       bytes, err := read(op.inner, path)
+       if err != nil {
+               return nil, err
+       }
+
+       data := parseBytes(bytes)
+       if len(data) > 0 {
+               free := getFFI[bytesFree](op.ctx, symBytesFree)
+               free(bytes)
+
+       }
+       return data, nil
+}
+
+// Reader creates a new Reader for reading the contents of a file at the 
specified path.
+//
+// This function is a wrapper around the C-binding function 
`opendal_operator_reader`.
+//
+// # Parameters
+//
+//   - path: The path of the file to read.
+//
+// # Returns
+//
+//   - *OperatorReader: A reader for accessing the file's contents. It 
implements `io.ReadCloser`.
+//   - error: An error if the reader creation fails, or nil if successful.
+//
+// # Notes
+//
+//   - This implementation does not support the `reader_with` functionality.
+//   - The returned reader allows for more controlled and efficient reading of 
large files.
+//
+// # Example
+//
+//     func exampleReader(op *opendal.Operator) {
+//             r, err := op.Reader("path/to/file")
+//             if err != nil {
+//                     log.Fatal(err)
+//             }
+//             defer r.Close()
+//
+//             size := 1024 // Read 1KB at a time
+//             buffer := make([]byte, size)
+//
+//             for {
+//                     n, err := r.Read(buffer)
+//                     if err != nil {
+//                             log.Fatal(err)
+//                     }
+//                     fmt.Printf("Read %d bytes: %s\n", n, buffer[:n])
+//             }
+//     }
+//
+// Note: This example assumes proper error handling and import statements.
+func (op *Operator) Reader(path string) (*OperatorReader, error) {
+       getReader := getFFI[operatorReader](op.ctx, symOperatorReader)
+       inner, err := getReader(op.inner, path)
+       if err != nil {
+               return nil, err
+       }
+       reader := &OperatorReader{
+               inner: inner,
+               op:    op,
+       }
+       return reader, nil
+}
+
+type OperatorReader struct {

Review Comment:
   Can we just use `Reader`? User will use this as `opendal.Reader`, right?



##########
bindings/go/README.md:
##########
@@ -2,60 +2,123 @@
 
 ![](https://img.shields.io/badge/status-unreleased-red)
 
-opendal-go requires opendal-c to be installed.
+opendal-go is a **Native** support Go binding without CGO enabled and is built 
on top of opendal-c.
 
-```shell
-cd bindings/c
-make build
+```bash
+go get opendal.apache.org/go@latest
 ```
 
-You will find `libopendal_c.so` under `{root}/target`.
+opendal-go requires **libffi** to be installed.
 
-Then, we need to add a `opendal_c.pc` files
+## Basic Usage
 
-```pc
-libdir=/path/to/opendal/target/debug/
-includedir=/path/to/opendal/bindings/c/include/
+```go
+package main
 
-Name: opendal_c
-Description: opendal c binding
-Version:
+import (
+       "fmt"
+       "os"
 
-Libs: -L${libdir} -lopendal_c
-Cflags: -I${includedir}
-```
+       "github.com/yuchanns/opendal-go-services/memory"
+       "go.yuchanns.xyz/opendal"
+)
 
-And set the `PKG_CONFIG_PATH` environment variable to the directory where 
`opendal_c.pc` is located.
+func main() {
+       // Initialize a new in-memory operator
+       op, err := opendal.NewOperator(memory.Scheme, opendal.OperatorOptions{})
+       if err != nil {
+               panic(err)
+       }
+       defer op.Close()
 
-```shell
-export PKG_CONFIG_PATH=/dir/of/opendal_c.pc
-```
+       // Write data to a file named "test"
+       err = op.Write("test", []byte("Hello opendal go binding!"))
+       if err != nil {
+               panic(err)
+       }
 
-Then, we can build the go binding.
+       // Read data from the file "test"
+       data, err := op.Read("test")
+       if err != nil {
+               panic(err)
+       }
+       fmt.Printf("Read content: %s\n", data)
 
-```shell
-cd bindings/go
-go build -tags dynamic .
-```
+       // List all entries under the root directory "/"
+       lister, err := op.List("/")
+       if err != nil {
+               panic(err)
+       }
+       defer lister.Close()
 
-To running the go binding tests, we need to tell the linker where to find the 
`libopendal_c.so` file.
+       // Iterate through all entries
+       for lister.Next() {
+               entry := lister.Entry()
 
-```shell
-expose LD_LIBRARY_PATH=/path/to/opendal/bindings/c/target/debug/
-```
+               // Get entry name (not used in this example)
+               _ = entry.Name()
+
+               // Get metadata for the current entry
+               meta, _ := op.Stat(entry.Path())
+
+               // Print file size
+               fmt.Printf("Size: %d bytes\n", meta.ContentLength())
 
-Then, we can run the tests.
+               // Print last modified time
+               fmt.Printf("Last modified: %s\n", meta.LastModified())
 
-```shell
-go test -tags dynamic .
+               // Check if the entry is a directory or a file
+               fmt.Printf("Is directory: %v, Is file: %v\n", meta.IsDir(), 
meta.IsFile())
+       }
+
+       // Check for any errors that occurred during iteration
+       if err := lister.Error(); err != nil {
+               panic(err)
+       }
+
+       // Copy a file
+       op.Copy("test", "test_copy")
+
+       // Rename a file
+       op.Rename("test", "test_rename")
+
+       // Delete a file
+       op.Delete("test_rename")
+}
 ```
 
-For benchmark
+## Run Tests
 
-```shell
-go test -bench=. -tags dynamic .
+```bash
+# Run all tests
+CGO_ENABLE=0 go test -v -run TestBehavior

Review Comment:
   `CGO_ENABLE=0`!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to