ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.