A File Allocation Table implementation written in Go. Intended for use in embedded systems with SD cards, USBs, MMC devices and also usable with an in-RAM representation. Inspired by FatFs.
This is a Work in Progress.
How to install package with newer versions of Go (+1.16):
go mod download github.com/soypat/fat@latest
The following example does the following:
- Mounts a FAT filesystem to the
fat.FS
type. - Creates a new empty file called
newfile.txt
, replacing any existing file. - Writes
Hello, World!
to that file. - Closes the file to synchronize pending changes to the FAT filesystem.
- Opens the file in read mode and reads all of it's contents and prints them to standard output.
package main
import "github.com/soypat/fat"
func main() {
// device could be an SD card, RAM, or anything that implements the BlockDevice interface.
device := NewFATDevice()
var fs fat.FS
err := fs.Mount(device, device.BlockSize(), fat.ModeRW)
if err != nil {
panic(err)
}
var file fat.File
err = fs.OpenFile(&file, "newfile.txt", fat.ModeCreateAlways|fat.ModeWrite)
if err != nil {
panic(err)
}
_, err = file.Write([]byte("Hello, World!"))
if err != nil {
panic(err)
}
err = file.Close()
if err != nil {
panic(err)
}
// Read back the file:
err = fs.OpenFile(&file, "newfile.txt", fat.ModeRead)
if err != nil {
panic(err)
}
data, err := io.ReadAll(&file)
if err != nil {
panic(err)
}
fmt.Println(string(data))
file.Close()
// Output: Hello, World!
}