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


##########
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:
   Make sense.



-- 
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