1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat(fileutil): add ReadFile func (#140)

This commit is contained in:
o98k
2023-09-23 13:45:02 +08:00
committed by GitHub
parent 073c77e751
commit 781b89d51a
5 changed files with 138 additions and 0 deletions

View File

@@ -47,6 +47,7 @@ import (
- [WriteCsvFile](#WriteCsvFile)
- [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile)
- [ReadFile](#ReadFile)
<div STYLE="page-break-after: always;"></div>
@@ -839,3 +840,41 @@ func main() {
// hello
}
```
### <span id="ReadFile">ReadFile</span>
<p>读取文件或者URL</p>
<b>函数签名:</b>
```go
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
```
<b>示例:<span style="float:right;display:inline-block;"></span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
return
}
defer fn()
dat, err := io.ReadAll(reader)
if err != nil {
return
}
fmt.Println(string(dat))
// Output:
// User-agent: *
// Disallow: /deny
}
```