path/filepath Package का उपयोग
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Extracting the Base and Directory
filepath.Base किसी path का सिर्फ आखिरी element (आमतौर पर file name) return करता है, जबकि filepath.Dir इससे पहले की हर चीज़ return करता है -- एक पूरे path को इसकी directory और filename हिस्सों में बांटने के लिए उपयोगी।
उदाहरण: Extracting the Base and Directory
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Getting a File Extension
filepath.Ext किसी path का file extension return करता है, leading dot सहित, जो file type के आधार पर logic branch करने के लिए सुविधाजनक है (जैसे .csv बनाम .json के लिए एक parser चुनना)।
उदाहरण: Getting a File Extension
// 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))
}
}
Login to try C/C++/Java/PHP code in the editor
- paths को manually string concatenation और '/' से जोड़ना, जो Windows पर टूट जाता है जहां separator '\\' है।
- filepath.Base (आखिरी path element) को filepath.Dir (आखिरी element के अलावा बाकी सब) के साथ confuse करना।
- यह मान लेना कि 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: