Golang os.O_RDWR Constant with Examples

Golang | os.O_RDWR Constant: Here, we are going to learn about the O_RDWR constant of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 23, 2021

os.O_RDWR Constant

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The O_RDWR constant is used to specify the read-write mode to open a file in the read-write mode.

The value of O_RDWR constant 2.

Syntax:

O_RDWR int

Implementation in the package source code:

const O_RDWR int = syscall.O_RDWR

Parameter(s):

  • None

Return Value:

The return type of the os.O_RDWR constant is an int, it returns 2 i.e., the value of the O_RDWR constant is 2.

Example 1:

// Golang program to demonstrate the
// example of O_RDWR constant

package main

import (
	"fmt"
	"os"
)

func main() {
	// Printing the type and value
	fmt.Printf("Type of os.O_RDWR: %T\n",
		os.O_RDWR)
	fmt.Printf("Value of os.O_RDWR: %d\n",
		os.O_RDWR)
}

Output:

Type of os.O_RDWR: int
Value of os.O_RDWR: 2

Example 2:

// Golang program to demonstrate the
// example of O_RDWR constant

package main

import (
	"fmt"
	"os"
)

func main() {
	// Opening a file in read-write
	f, err := os.OpenFile("file.txt", os.O_RDWR, 0755)
	fmt.Println(f, ",", err)
}

Output:

RUN 1: (If file exists)
&{0xc00005c180} , <nil>

RUN 2: (If file doesn't exist)
<nil> , open file.txt: no such file or directory

Golang os Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.