github.com/iceber/iouring-go@v0.0.0-20230403020409-002cfd2e2a90/examples/link/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/iceber/iouring-go"
     8  )
     9  
    10  const entries uint = 64
    11  
    12  var (
    13  	str1 = "str1 str1 str1 str1\n"
    14  	str2 = "str2 str2 str2 str2 str2\n"
    15  )
    16  
    17  func main() {
    18  	iour, err := iouring.New(entries)
    19  	if err != nil {
    20  		panic(fmt.Sprintf("new IOURing error: %v", err))
    21  	}
    22  	defer iour.Close()
    23  
    24  	file, err := os.Create("./tmp")
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	prepWrite1 := iouring.Write(int(file.Fd()), []byte(str1)).WithInfo("write str1")
    30  	prepWrite2 := iouring.Pwrite(int(file.Fd()), []byte(str2), uint64(len(str1))).WithInfo("write str2")
    31  
    32  	buffer := make([]byte, len(str1)+len(str2))
    33  	prepRead1 := iouring.Read(int(file.Fd()), buffer).WithInfo("read fd to buffer")
    34  	prepRead2 := iouring.Write(int(os.Stdout.Fd()), buffer).WithInfo("read buffer to stdout")
    35  
    36  	ch := make(chan iouring.Result, 4)
    37  	_, err = iour.SubmitLinkRequests(
    38  		[]iouring.PrepRequest{
    39  			prepWrite1,
    40  			prepWrite2,
    41  			prepRead1,
    42  			prepRead2,
    43  		},
    44  		ch,
    45  	)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	for i := 0; i < 4; i++ {
    51  		result := <-ch
    52  		info := result.GetRequestInfo().(string)
    53  		fmt.Println(info)
    54  		if err := result.Err(); err != nil {
    55  			fmt.Printf("error: %v\n", err)
    56  		}
    57  	}
    58  }