Skip to content

Instantly share code, notes, and snippets.

@jrockway
Created March 29, 2021 18:53
Show Gist options
  • Save jrockway/19b341f509b06732dda5d21829b9ba0d to your computer and use it in GitHub Desktop.
Save jrockway/19b341f509b06732dda5d21829b9ba0d to your computer and use it in GitHub Desktop.
Make a bunch of files
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
var (
n = flag.Int("n", 1, "number of files to create")
size = flag.Int64("size", 1*1024*1024*1024, "total size of output files to create in bytes")
)
type charReader struct{}
func (_ charReader) Read(b []byte) (int, error) {
var n int
for i := range b {
b[i] = 'A'
n++
}
return n, nil
}
func main() {
flag.Parse()
if err := os.MkdirAll("out", 0775); err != nil {
log.Fatal(err)
}
each := *size / int64(*n)
for i := 0; i < *n; i++ {
name := filepath.Join("out", fmt.Sprintf("data-%d", i))
fd, err := os.OpenFile(name, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
}
n, err := io.Copy(fd, io.LimitReader(charReader{}, each))
log.Printf("%s: wrote %v bytes\n", name, n)
if err != nil {
log.Fatal(err)
}
if err := fd.Close(); err != nil {
log.Fatal(err)
}
}
fmt.Printf("wrote %d files of %d bytes each totalling %d bytes\n", *n, each, *size)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment