github.com/haraldrudell/parl@v0.4.176/pos/append-to-file.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pos
     7  
     8  import (
     9  	"os"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  )
    13  
    14  const (
    15  	// when created, output file permissions is user-read/write
    16  	FilePermUrw os.FileMode = 0o600 // rw- --- ---
    17  	// flags for os.OpenFile: must be new, write-only
    18  	openFlagsCreateOrAppend = os.O_CREATE | os.O_APPEND | os.O_WRONLY
    19  )
    20  
    21  func AppendToFile(filename string) (osFile *os.File, err error) {
    22  	if osFile, err = os.OpenFile(filename, openFlagsCreateOrAppend, FilePermUrw); err != nil {
    23  		err = perrors.ErrorfPF("OpenFile: %w", err)
    24  	}
    25  
    26  	return
    27  }