Home »
Golang »
Golang Reference
Golang os.O_CREATE Constant with Examples
Golang | os.O_CREATE Constant: Here, we are going to learn about the O_CREATE constant of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 23, 2021
os.O_CREATE
In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The O_CREATE constant is used to create a file if none exists.
The value of the O_CREATE constant is 64.
Syntax
O_CREATE int
Implementation in the package source code:
const O_CREATE int = syscall.O_CREATE
Parameters
Return Value
The return type of the os.O_CREATE constant is an int, it returns 64 i.e., the value of the O_CREATE constant is 64.
Example 1
// Golang program to demonstrate the
// example of O_CREATE constant
package main
import (
"fmt"
"os"
)
func main() {
// Printing the type and value
fmt.Printf("Type of os.O_CREATE: %T\n",
os.O_CREATE)
fmt.Printf("Value of os.O_CREATE: %d\n",
os.O_CREATE)
}
Output:
Type of os.O_CREATE: int
Value of os.O_CREATE: 64
Example 2
// Golang program to demonstrate the
// example of O_CREATE constant
package main
import (
"fmt"
"os"
)
func main() {
// Creating a file
f, err := os.OpenFile("file.txt", os.O_CREATE, 0755)
fmt.Println(f, ",", err)
}
Output:
RUN 1: (If file exists)
&{0xc000056180} , <nil>
RUN 2: (RUN 1: (If file doesn't exist)
&{0xc000056180} , <nil>
Golang os Package »