forked from mirror/afero
Format Readme.md code blocks as Go code
This commit is contained in:
parent
3a224e9827
commit
55789de153
28
README.md
28
README.md
|
@ -51,20 +51,20 @@ First use go get to install the latest version of the library.
|
|||
$ go get github.com/spf13/afero
|
||||
|
||||
Next include Afero in your application.
|
||||
|
||||
```go
|
||||
import "github.com/spf13/afero"
|
||||
|
||||
```
|
||||
|
||||
## Step 2: Declare a backend
|
||||
|
||||
First define a package variable and set it to a pointer to a filesystem.
|
||||
|
||||
```go
|
||||
var AppFs afero.Fs = &afero.MemMapFs{}
|
||||
|
||||
or
|
||||
|
||||
var AppFs afero.Fs = &afero.OsFs{}
|
||||
|
||||
```
|
||||
It is important to note that if you repeat the composite literal you
|
||||
will be using a completely new and isolated filesystem. In the case of
|
||||
OsFs it will still use the same underlying filesystem but will reduce
|
||||
|
@ -76,9 +76,9 @@ Throughout your application use any function and method like you normally
|
|||
would.
|
||||
|
||||
So if my application before had:
|
||||
|
||||
```go
|
||||
os.Open('/tmp/foo')
|
||||
|
||||
```
|
||||
We would replace it with a call to `AppFs.Open('/tmp/foo')`.
|
||||
|
||||
`AppFs` being the variable we defined above.
|
||||
|
@ -87,7 +87,7 @@ We would replace it with a call to `AppFs.Open('/tmp/foo')`.
|
|||
## List of all available functions
|
||||
|
||||
File System Methods Available:
|
||||
|
||||
```go
|
||||
Chmod(name string, mode os.FileMode) : error
|
||||
Chtimes(name string, atime time.Time, mtime time.Time) : error
|
||||
Create(name string) : File, error
|
||||
|
@ -100,9 +100,9 @@ File System Methods Available:
|
|||
RemoveAll(path string) : error
|
||||
Rename(oldname, newname string) : error
|
||||
Stat(name string) : os.FileInfo, error
|
||||
|
||||
```
|
||||
File Interfaces and Methods Available:
|
||||
|
||||
```go
|
||||
io.Closer
|
||||
io.Reader
|
||||
io.ReaderAt
|
||||
|
@ -117,7 +117,7 @@ File Interfaces and Methods Available:
|
|||
Sync() : error
|
||||
Truncate(size int64) : error
|
||||
WriteString(s string) : ret int, err error
|
||||
|
||||
```
|
||||
In some applications it may make sense to define a new package that
|
||||
simply exports the file system variable for easy access from anywhere.
|
||||
|
||||
|
@ -146,7 +146,7 @@ appropriate in my application code. This approach ensures that Tests are order
|
|||
independent, with no test relying on the state left by an earlier test.
|
||||
|
||||
Then in my tests I would initialize a new MemMapFs for each test:
|
||||
|
||||
```go
|
||||
func TestExist(t *testing.T) {
|
||||
appFS = &afero.MemMapFs{}
|
||||
// create test files and directories
|
||||
|
@ -170,7 +170,7 @@ Then in my tests I would initialize a new MemMapFs for each test:
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Using Afero with Http
|
||||
|
||||
|
@ -182,11 +182,11 @@ returns an http.File type.
|
|||
|
||||
Afero provides an httpFs file system which satisfies this requirement.
|
||||
Any Afero FileSystem can be used as an httpFs.
|
||||
|
||||
```go
|
||||
httpFs := &afero.HttpFs{SourceFs: <ExistingFS>}
|
||||
fileserver := http.FileServer(httpFs.Dir(<PATH>)))
|
||||
http.Handle("/", fileserver)
|
||||
|
||||
```
|
||||
# Available Backends
|
||||
|
||||
## OsFs
|
||||
|
|
Loading…
Reference in New Issue