package main import ( "fmt" "os" ) func main() { file, err := os.Open("example.txt") if err != nil { fmt.Println(err) return } defer file.Close() data := make([]byte, 100) count, err := file.Read(data) if err != nil { fmt.Println(err) return } fmt.Printf("Read %d bytes: %q\n", count, data[:count]) }
package main import ( "fmt" "strings" ) func main() { str := "hello world\n" reader := strings.NewReader(str) data := make([]byte, 100) count, err := reader.Read(data) if err != nil { fmt.Println(err) return } fmt.Printf("Read %d bytes: %q\n", count, data[:count]) }In this example, we create a string reader using the strings.NewReader function and read up to 100 bytes of data from it using the Read method. We then print out the number of bytes read and the actual data. This code example uses the "strings" package library.