×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang os.O_WRONLY Constant with Examples

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

os.O_WRONLY Constant

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

The value of O_WRONLY constant 1.

Syntax

O_WRONLY int

Implementation in the package source code:

const O_WRONLY int = syscall.O_WRONLY

Parameters

  • None

Return Value

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

Example 1

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

package main

import (
	"fmt"
	"os"
)

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

Output:

Type of os.O_WRONLY: int
Value of os.O_WRONLY: 1

Example 2

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

package main

import (
	"fmt"
	"os"
)

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

Output:

RUN 1: (If file exists)
&{0xc000078120} , <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.