Last active
March 17, 2020 10:16
-
-
Save rohitpaulk/b67df434521772fdef56a5ba9f0acc69 to your computer and use it in GitHub Desktop.
Write a huge file in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
f, err := os.Create("/tmp/dat2") | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
fmt.Println("Writing data...") | |
gbs := 30 // How many GBs you want to write | |
kb := strings.Repeat("1234567890", 100) // 10 bytes * 100 = 1KB string | |
mb := strings.Repeat(kb, 1000) // 1000 bytes * 1KB = 1MB string | |
for i := 0; i < gbs*1000; i++ { | |
_, err := f.Write([]byte(mb)) | |
if err != nil { | |
panic(err) | |
} | |
if i%100 == 0 { | |
fmt.Printf("Wrote %d/%dk MBs\n", i, gbs) | |
} | |
} | |
fmt.Println("Writing done.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment