go 基本IO接口
Posted saryli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go 基本IO接口相关的知识,希望对你有一定的参考价值。
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
以上是关于go 基本IO接口的主要内容,如果未能解决你的问题,请参考以下文章