在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 |
请发表评论