To open a file in Golang, you can use the “os.OpenFile()” function. The os.Open() function takes a filepath, flag, and file mode as arguments and opens and reads the file.
Syntax
os.OpenFile(name/path string, flag int, perm FileMode)
Parameters
name: The first parameter is a file name, which is the complete file path of that file.
If the file does not exist, and the O_CREATE flag is passed, it is created with mode perm (before umask). If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.
Example 1
package main
import (
"fmt"
"os"
)
var path = "app.txt"
func isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}
func main() {
fmt.Println("Opening a file ")
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if isError(err) {
return
}
defer file.Close()
}
Output
Opening a file
Example 2
package main
import (
"fmt"
"os"
)
var path = "appss.txt"
func isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}
func main() {
fmt.Println("Opening a file ")
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if isError(err) {
return
}
defer file.Close()
}
Output
Opening a file open
appss.txt: no such file or directory
Example 3
We can solve this problem and create a file on the fly while opening a file, but we need to change the os parameters.OpenFile() function. We need to change file permission and flag.
package main
import (
"fmt"
"os"
)
var path = "appss.txt"
func isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}
func main() {
fmt.Println("Opening a file ")
var file, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if isError(err) {
return
}
defer file.Close()
}
See the os.OpenFile() function closely.
var file, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
Output
Opening a file
OS Package OpenFile Flags
The Golang OS package OpenFile function opens files using flags and permissions. One of the first three flags below must be provided in the OpenFile function.
Flag | Meaning |
---|---|
O_RDONLY | It opens the file read-only |
O_WRONLY | It opens the file write-only |
O_RDWR | It opens the file read-write |
O_APPEND | It appends data to the file when writing |
O_CREATE | It creates a new file if none exists |
O_EXCL | It is used with O_CREATE, and the file must not exist |
O_SYNC | It opens for synchronous I/O |
O_TRUNC | If possible, truncate the file when opened |
You can use the different flags per your requirement while opening a file.
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.