← Back to Go Course | Chapter 13: File IO & OS | Lesson 5 of 7

path/filepath Package का उपयोग

filepath file paths को सही से बनाने और अलग करने के लिए Go का toolkit है, ताकि आपका program Windows या Linux पर चलने पर भी एक जैसा काम करे।
Syntax
go
path := filepath.Join("dir", "file")
filepath.Base(path)
filepath.Ext(path)
filepath.Walk(root, walkFn)

Joining Path Segments

filepath.Join कई path segments को एक में combine करता है, operating system के path separator को सही से उपयोग करते हुए और अनावश्यक slashes साफ़ करते हुए -- manual string concatenation पर हमेशा प्राथमिकता दी जाती है।

उदाहरण: Joining Path Segments

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"path/filepath"
)

// main is where the program starts running
func main() {
	// Short declaration: create path and infer the type
	path := filepath.Join("data", "reports", "2024", "summary.csv")
	// Print the values followed by a newline
	fmt.Println(path)
}

Extracting the Base and Directory

filepath.Base किसी path का सिर्फ आखिरी element (आमतौर पर file name) return करता है, जबकि filepath.Dir इससे पहले की हर चीज़ return करता है -- एक पूरे path को इसकी directory और filename हिस्सों में बांटने के लिए उपयोगी।

उदाहरण: Extracting the Base and Directory

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"path/filepath"
)

// main is where the program starts running
func main() {
	// Short declaration: create path and infer the type
	path := "data/reports/summary.csv"
	// Print the values followed by a newline
	fmt.Println("base:", filepath.Base(path))
	// Print the values followed by a newline
	fmt.Println("dir:", filepath.Dir(path))
}

Getting a File Extension

filepath.Ext किसी path का file extension return करता है, leading dot सहित, जो file type के आधार पर logic branch करने के लिए सुविधाजनक है (जैसे .csv बनाम .json के लिए एक parser चुनना)।

उदाहरण: Getting a File Extension

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"path/filepath"
)

// main is where the program starts running
func main() {
	// Short declaration: create files and infer the type
	files := []string{"report.csv", "image.png", "README"}
	// Loop over files; the blank identifier _ ignores the index
	for _, f := range files {
		// Print the values followed by a newline
		fmt.Println(f, "->", filepath.Ext(f))
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. paths को manually string concatenation और '/' से जोड़ना, जो Windows पर टूट जाता है जहां separator '\\' है।
  2. filepath.Base (आखिरी path element) को filepath.Dir (आखिरी element के अलावा बाकी सब) के साथ confuse करना।
  3. यह मान लेना कि filepath.Ext पूरे file name को शामिल करता है -- यह सिर्फ extension return करता है, आखिरी dot से शुरू होते हुए।
चैप्टर सारांश
  • filepath.Join सही OS-specific separator उपयोग करके हिस्सों से एक path बनाता है।
  • filepath.Base किसी path का आखिरी element return करता है; filepath.Dir इससे पहले की हर चीज़ return करता है।
  • filepath.Ext किसी file का extension return करता है, leading dot सहित।
  • manual string concatenation के बजाय filepath उपयोग करना path code को operating systems में portable बनाता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.