×

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_EXCL Constant with Examples

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

os.O_EXCL

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The O_EXCL constant is used to create a file if the file does not exist. It is used with the O_CREATE constant. Please note that the file must not exist.

The value of the O_EXCL constant is 128.

Syntax

O_EXCL int

Implementation in the package source code:

const O_EXCL int = syscall.O_EXCL

Parameters

  • None

Return Value

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

Example 1

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

package main

import (
	"fmt"
	"os"
)

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

Output:

Type of os.O_EXCL: int
Value of os.O_EXCL: 128

Example 2

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

package main

import (
	"fmt"
	"os"
)

func main() {
	// Creating a file, file must not exist
	f, err := os.OpenFile("file.txt",
		os.O_CREATE|os.O_EXCL, 0755)
	fmt.Println(f, ",", err)
}

Output:

RUN 1: (If file exists)
<nil> , open file.txt: file exists

RUN 2: (RUN 1: (If file doesn't exist)
&{0xc000056180} , <nil>

Golang os Package »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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