Golang os.PathSeparator Constant with Examples

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

os.PathSeparator

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The PathSeparator constant is used to get the operating specific path separator. The value of PathSeparator constant may be different based on the operating system.

Syntax:

PathSeparator int32

Implementation in the package source code:

PathSeparator     = '/' 
// Here, '/' is Unix-based path separator

Parameter(s):

  • None

Return Value:

The return type of the os.PathSeparator constant is an int32, it returns '/' (Unix-based operating system) i.e., the value of PathSeparator constant is '/' on Unix operating system.

Example 1:

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

package main

import (
	"fmt"
	"os"
)

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

Output:

Type of os.PathSeparator: int32
Value of os.PathSeparator: /

Example 2:

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

package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	// Making a cross-platform file path
	// Example:
	// For unix: 'dir/example',
	// For windows: 'dir\example'

	path1 := "dir" + string(os.PathSeparator) + "example"
	fmt.Println("path1: " + path1)

	path2 := filepath.FromSlash("dir/example")
	fmt.Println("FromSlash (path1): " + path2)
}

Output:

path1: dir/example
FromSlash (path1): dir/example

Golang os Package »





Comments and Discussions!

Load comments ↻






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