Skip to content

Commit e6e6d13

Browse files
committed
add example to README
1 parent 1228bb4 commit e6e6d13

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Diff for: README.md

+52
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,55 @@ How to install package with newer versions of Go (+1.16):
1515
```sh
1616
go mod download github.com/soypat/fat@latest
1717
```
18+
19+
### Basic usage example
20+
21+
The following example does the following:
22+
1. Mounts a FAT filesystem to the `fat.FS` type.
23+
2. Creates a new empty file called `newfile.txt`, replacing any existing file.
24+
3. Writes `Hello, World!` to that file.
25+
4. Closes the file to synchronize pending changes to the FAT filesystem.
26+
5. Opens the file in read mode and reads all of it's contents and prints them to standard output.
27+
28+
```go
29+
package main
30+
31+
import "github.com/soypat/fat"
32+
33+
func main() {
34+
// device could be an SD card, RAM, or anything that implements the BlockDevice interface.
35+
device := NewFATDevice()
36+
var fs fat.FS
37+
err := fs.Mount(device, device.BlockSize(), fat.ModeRW)
38+
if err != nil {
39+
panic(err)
40+
}
41+
var file fat.File
42+
err = fs.OpenFile(&file, "newfile.txt", fat.ModeCreateAlways|fat.ModeWrite)
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
_, err = file.Write([]byte("Hello, World!"))
48+
if err != nil {
49+
panic(err)
50+
}
51+
err = file.Close()
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
// Read back the file:
57+
err = fs.OpenFile(&file, "newfile.txt", fat.ModeRead)
58+
if err != nil {
59+
panic(err)
60+
}
61+
data, err := io.ReadAll(&file)
62+
if err != nil {
63+
panic(err)
64+
}
65+
fmt.Println(string(data))
66+
file.Close()
67+
// Output: Hello, World!
68+
}
69+
```

0 commit comments

Comments
 (0)