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

Files को Write करना

एक file लिखना एक note लिखने और disk पर एक envelope में seal करने जैसा है ताकि आपका program चलना खत्म करने के बाद भी यह वहां रहे।
Syntax
go
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

markup
// 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")
}

Writing Incrementally with os.Create

os.Create लिखने के लिए एक file खोलता है, अगर मौजूद न हो तो इसे बनाते हुए या अगर मौजूद हो तो इसे truncate करते हुए, और एक *os.File return करता है जिस पर आप बंद करने से पहले कई बार Write call कर सकते हैं।

Note: buffered writes को disk पर flush होने की पुष्टि के लिए हमेशा defer file.Close() करें।

उदाहरण: Writing Incrementally with os.Create

markup
// 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")
}

Appending to an Existing File

पहले से मौजूद data मिटाए बिना data जोड़ने के लिए, file को os.OpenFile से O_APPEND और O_WRONLY flags उपयोग करके खोलें, बजाय os.Create के जो हमेशा file को खाली शुरू करता है।

उदाहरण: Appending to an Existing File

markup
// 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))
}
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. os.WriteFile को permission bits argument (जैसे 0644) देना भूल जाना, जो नियंत्रित करता है कि कौन file पढ़/लिख सकता है।
  2. किसी मौजूदा file में append करने का इरादा होने पर os.Create से file खोलना -- Create हमेशा truncate करता है।
  3. लिखने के लिए खोली गई 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:

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.