- Hands-On System Programming with Go
- Alex Guerrieri
- 62字
- 2021-06-24 13:42:28
Truncate
To truncate the content of a file under a certain dimension, and leave the file untouched if it's smaller, there is the os.Truncate method. Its usage is pretty simple, as shown in the following code:
package main
import "os"
func main() {
// let's keep thing under 4kB
if err := os.Truncate("file.txt", 4096); err != nil {
fmt.Println("Error:", err)
}
}