×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang os.O_RDONLY Constant with Examples

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

os.O_RDONLY Constant

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

The value of O_RDONLY constant 0.

Syntax

O_RDONLY int

Implementation in the package source code:

const O_RDONLY int = syscall.O_RDONLY

Parameters

  • None

Return Value

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

Example 1

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

package main

import (
	"fmt"
	"os"
)

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

Output:

Type of os.O_RDONLY: int
Value of os.O_RDONLY: 0

Example 2

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

package main

import (
	"fmt"
	"os"
)

func main() {
	// Opening a file in read-only
	f, err := os.OpenFile("file.txt", os.O_RDONLY, 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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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