10 examples of 'golang open file for writing' in Go

Every line of 'golang open file for writing' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
122func openWrite(c *Command, w io.Writer, fd int) (io.Writer, error) {
123 if c.fdmap[fd] != "" {
124 f, err := os.Create(c.fdmap[fd])
125 c.files[fd] = f
126 return f, err
127 }
128 return w, nil
129}
146func (mif *ModuleInfoFile) openWriter() bool {
147 if mif.mode != Writer && mif.mode != ReaderWriter {
148 return false
149 }
150
151 ioutil.WriteFile(mif.fnModuleInfo+".lock", []byte("0"), 0666)
152
153 _, err := os.Stat(mif.fnModuleInfo)
154 if os.IsNotExist(err) {
155 mif.ffModuleInfoW, err = os.Create(mif.fnModuleInfo)
156 if err != nil {
157 log.Println("@ERR CREATING ffModuleInfoW: ", err)
158 return false
159 }
160 mif.isWriterOpen = true
161
162 return true
163 } else if err == nil {
164 mif.ffModuleInfoW, err = os.OpenFile(mif.fnModuleInfo, os.O_RDWR, 0666)
165 if err != nil {
166 log.Println("@ERR OPENING ffModuleInfoW: ", err)
167 return false
168 }
169 return true
170 }
171
172 return false
173}
60func ReadOpen(path string) (*os.File, error) {
61 flag := os.O_RDONLY
62 perm := os.FileMode(0)
63 return os.OpenFile(path, flag, perm)
64}
354func openOSFile(filePath string) (*os.File, error) {
355
356 // Ensure the path exists before opening the file, NoOp if dir already exists.
357 var fileMode os.FileMode = 0666
358 if err := os.MkdirAll(path.Dir(filePath), os.ModeDir|0777); err != nil {
359 return nil, err
360 }
361
362 file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, fileMode)
363 return file, err
364}
169func (u *FileSystemUtil) OpenFileWriter(path string) (*os.File, error) {
170 return os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
171}
476func writeFile(data []byte, path string) {
477 f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
478 if err != nil {
479 fmt.Fprintf(os.Stderr, "Could not create file %s", path)
480 os.Exit(1)
481 }
482 f.Write(data)
483}
61func openOrCreate(filePath string) (*os.File, error) {
62 return os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0600)
63}
123func (fa *SecureFileAccessor) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
124
125 if perm&(^fa.MaxPermissions&0777) != 0 {
126 abort("Requested opening file in a too permissive mode: '%s' (max permissions '%s')", perm, fa.MaxPermissions)
127 }
128 return os.OpenFile(name, flag, perm)
129}
24func CXOpenFile(filename string) (*os.File, error) {
25 filename = filepath.Join(workingDir, filename)
26
27 if logFile {
28 fmt.Printf("CXOpenFile: Opening '%s'\n", filename)
29 }
30
31 file, err := os.Open(filename)
32 if logFile && err != nil {
33 fmt.Printf("CXOpenFile: Failed to open '%s': %v\n", filename, err)
34 }
35 return file, err
36}
97func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
98 // roll log files by default
99 if fw.Roll == nil || *fw.Roll {
100 if fw.RollSizeMB == 0 {
101 fw.RollSizeMB = 100
102 }
103 if fw.RollCompress == nil {
104 compress := true
105 fw.RollCompress = &compress
106 }
107 if fw.RollKeep == 0 {
108 fw.RollKeep = 10
109 }
110 if fw.RollKeepDays == 0 {
111 fw.RollKeepDays = 90
112 }
113
114 return &lumberjack.Logger{
115 Filename: fw.Filename,
116 MaxSize: fw.RollSizeMB,
117 MaxAge: fw.RollKeepDays,
118 MaxBackups: fw.RollKeep,
119 LocalTime: fw.RollLocalTime,
120 Compress: *fw.RollCompress,
121 }, nil
122 }
123
124 // otherwise just open a regular file
125 return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
126}

Related snippets