Files को Write करना
In this page:
err := os.WriteFile("path", data, 0644)
f, err := os.Create("path")
defer f.Close()
f.WriteString("text")
Writing a File in One Call
os.WriteFile दिए गए byte slice को इसकी पूरी contents के रूप में उपयोग करके एक file बनाता है (या overwrite करता है), एक सुविधाजनक call में -- थोड़ी मात्रा में data save करने का सबसे सरल तरीका।
उदाहरण: Writing a File in One Call
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"io/ioutil"
)
// main is where the program starts running
func main() {
// Short declaration: create err and infer the type
err := ioutil.WriteFile("output.txt", []byte("Go makes file I/O simple"), 0644)
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("error:", err)
return
}
// Print the values followed by a newline
fmt.Println("file written successfully")
}
Login to try C/C++/Java/PHP code in the editor
Writing Incrementally with os.Create
os.Create लिखने के लिए एक file खोलता है, अगर मौजूद न हो तो इसे बनाते हुए या अगर मौजूद हो तो इसे truncate करते हुए, और एक *os.File return करता है जिस पर आप बंद करने से पहले कई बार Write call कर सकते हैं।
उदाहरण: Writing Incrementally with os.Create
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"os"
)
// main is where the program starts running
func main() {
// Short declaration: create f, err and infer the type
f, err := os.Create("log.txt")
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("error:", err)
return
}
// defer runs this call when the surrounding function returns
defer f.Close()
f.WriteString("first line\n")
f.WriteString("second line\n")
// Print the values followed by a newline
fmt.Println("wrote two lines")
}
Login to try C/C++/Java/PHP code in the editor
Appending to an Existing File
पहले से मौजूद data मिटाए बिना data जोड़ने के लिए, file को os.OpenFile से O_APPEND और O_WRONLY flags उपयोग करके खोलें, बजाय os.Create के जो हमेशा file को खाली शुरू करता है।
उदाहरण: Appending to an Existing File
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"io/ioutil"
"os"
)
// main is where the program starts running
func main() {
ioutil.WriteFile("history.txt", []byte("entry 1\n"), 0644)
// Short declaration: create f, err and infer the type
f, err := os.OpenFile("history.txt", os.O_APPEND|os.O_WRONLY, 0644)
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("error:", err)
return
}
// defer runs this call when the surrounding function returns
defer f.Close()
f.WriteString("entry 2\n")
// Short declaration: create data, _ and infer the type
data, _ := ioutil.ReadFile("history.txt")
fmt.Println(string(data))
}
Login to try C/C++/Java/PHP code in the editor
- os.WriteFile को permission bits argument (जैसे 0644) देना भूल जाना, जो नियंत्रित करता है कि कौन file पढ़/लिख सकता है।
- किसी मौजूदा file में append करने का इरादा होने पर os.Create से file खोलना -- Create हमेशा truncate करता है।
- लिखने के लिए खोली गई file को program exit होने से पहले बंद न करना, buffered data के disk तक कभी न पहुंचने का जोखिम उठाते हुए।
- os.WriteFile एक पूरी byte slice को एक call में किसी file में लिखता है, ज़रूरत के अनुसार इसे बनाते या truncate करते हुए।
- os.Create लिखने के लिए एक file खोलता है (या truncate करता है), एक explicit Close() मांगते हुए।
- File permission bits, जैसे 0644, Unix-like systems पर read/write/execute access control करते हैं।
- O_APPEND flag के साथ os.OpenFile किसी मौजूदा file के आखिर में data जोड़ने के लिए उपयोग होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: