Standard library / os
os.O_TRUNC
Truncates an existing regular file on opening for writing; destructive to old contents. Example uses a disposable temporary workspace. Expected stdout: '0\n'.
Formal Syntax & Structure
go syntaxgo
const O_TRUNC int = syscall.O_TRUNCWorking Example
Example Snippetgo
package main
import ( "fmt" "os")
func check(e error) { if e != nil { panic(e) }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() seed() f, e := os.OpenFile("a.txt", os.O_WRONLY|os.O_TRUNC, 0) check(e) check(f.Close()) i, e := os.Stat("a.txt") check(e) fmt.Println(i.Size())}