| 1 | # go-billy [](https://pkg.go.dev/github.com/go-git/go-billy/v5) [](https://github.com/go-git/go-billy/actions?query=workflow%3ATest) |
| 2 | |
| 3 | The missing interface filesystem abstraction for Go. |
| 4 | Billy implements an interface based on the `os` standard library, allowing to develop applications without dependency on the underlying storage. Makes it virtually free to implement mocks and testing over filesystem operations. |
| 5 | |
| 6 | Billy was born as part of [go-git/go-git](https://github.com/go-git/go-git) project. |
| 7 | |
| 8 | ## Version support |
| 9 | |
| 10 | go-billy v5 is in maintenance mode. Users should upgrade to [go-billy v6](https://pkg.go.dev/github.com/go-git/go-billy/v6) where possible. |
| 11 | |
| 12 | ## Installation |
| 13 | |
| 14 | ```go |
| 15 | import "github.com/go-git/go-billy/v5" // with go modules enabled (GO111MODULE=on or outside GOPATH) |
| 16 | import "github.com/go-git/go-billy" // with go modules disabled |
| 17 | ``` |
| 18 | |
| 19 | ## Usage |
| 20 | |
| 21 | Billy exposes filesystems using the |
| 22 | [`Filesystem` interface](https://pkg.go.dev/github.com/go-git/go-billy/v5?tab=doc#Filesystem). |
| 23 | Each filesystem implementation gives you a `New` method, whose arguments depend on |
| 24 | the implementation itself, that returns a new `Filesystem`. |
| 25 | |
| 26 | The following example caches in memory all readable files in a directory from any |
| 27 | billy's filesystem implementation. |
| 28 | |
| 29 | ```go |
| 30 | func LoadToMemory(origin billy.Filesystem, path string) (*memory.Memory, error) { |
| 31 | memory := memory.New() |
| 32 | |
| 33 | files, err := origin.ReadDir("/") |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | for _, file := range files { |
| 39 | if file.IsDir() { |
| 40 | continue |
| 41 | } |
| 42 | |
| 43 | src, err := origin.Open(file.Name()) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | dst, err := memory.Create(file.Name()) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | if _, err = io.Copy(dst, src); err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
| 57 | if err := dst.Close(); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | if err := src.Close(); err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return memory, nil |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | ## Why billy? |
| 71 | |
| 72 | The library billy deals with storage systems and Billy is the name of a well-known, IKEA |
| 73 | bookcase. That's it. |
| 74 | |
| 75 | ## License |
| 76 | |
| 77 | Apache License Version 2.0, see [LICENSE](LICENSE) |