标签:port int bsp file ret ase close fun nil
package main
import (
"fmt"
"io"
"strings"
)
func ReadFrom(reader io.Reader, num int) ([]byte, error) {
p := make([]byte, num)
n, err := reader.Read(p)
if n > 0 {
return p[:n], nil
}
return p, err
}
func sampleReaderFromString() {
data, _ := ReadFrom(strings.NewReader("from string"), 12)
fmt.Println(data)
}
func main() {
sampleReaderFromString()
}
输出:
[102 114 111 109 32 115 116 114 105 110 103]
package main
import (
"fmt"
"io"
"os"
"strings"
)
func sampleReadFile() {
file, _ := os.Open("main.go")
defer file.Close()
data, _ := ReadFrom(file, 20)
fmt.Println(string(data))
}
func ReadFrom(reader io.Reader, num int) ([]byte, error) {
p := make([]byte, num)
n, err := reader.Read(p)
if n > 0 {
return p[:n], nil
}
return p, err
}
func sampleReadStdin() {
fmt.Println("please input from stdin:")
data, _ := ReadFrom(os.Stdin, 11)
fmt.Println(data)
}
func sampleReaderFromString() {
data, _ := ReadFrom(strings.NewReader("from string"), 12)
fmt.Println(data)
}
func main() {
//sampleReaderFromString()
//ampleReadStdin()
sampleReadFile()
}
输出:
package main
import
标签:port int bsp file ret ase close fun nil
原文地址:https://www.cnblogs.com/saryli/p/11063334.html