Directory title
- 1. Writing files Writing files
-
- 1. Writing string to a file Writing string
- 2. Writing bytes to a file Writing bytes
- 3. Writing strings line by line to a file
- 4. Appending to a file Adding content
- 5. Writing to file concurrently Writing to file concurrently
- 2. iota constant generator
1. Writing files Writing files
1. Writing string to a file Writing string
package main import ( "fmt" "os" ) func main() {<!-- --> f, err := os.Create("writing.txt") // Create file fmt.Println(err) if err != nil {<!-- --> // Check if there is an error fmt.Println(err) return } l, err := f.WriteString("Hello, World!") // Write to file fmt.Println(err) if err != nil {<!-- --> // Check if there is an error fmt.Println(err) f.Close() return } fmt.Println(l, "bytes written successfully") err = f.Close() if err != nil {<!-- --> // Check if there is an error fmt.Println(err) return } } // <nil> // <nil> // 13 bytes written successfully
2. Writing bytes to a file Writing bytes
package main import ( "fmt" "os" ) func main() {<!-- --> f, err := os.Create("bytes") if err != nil {<!-- --> fmt.Println(err) return } d2 := []byte{<!-- -->104, 101, 108, 108, 111, 32, 98, 121, 116, 101, 115} n, err := f.Write(d2) if err != nil {<!-- --> fmt.Println(err) f.Close() return } fmt.Println(n, "Bytes written successfully") err = f.Close() if err != nil {<!-- --> fmt.Println(err) return } } \t\t // 11 Bytes written successfully
3. Writing strings line by line to a file Write string by line
package main import ( "fmt" "os" ) func main() {<!-- --> f, err := os.Create("Lines") if err != nil {<!-- --> fmt.Println(err) f.Close() return } d := []string{<!-- -->"Welcome to the world of Go1.", "Go is a compiled language.", "It is easy to learn Go."} for _, v := range d {<!-- --> fmt.Fprintln(f, v) // Use the fmt.Fprintln() function to write each element to the file if err != nil {<!-- --> fmt.Println(err) return } } err = f.Close() if err != nil {<!-- --> fmt.Println(err) return } fmt.Println("File written successfully") } // File written successfully
4. Appending to a file Add content
package main import ( "fmt" "os" ) func main() {<!-- --> f, err := os.OpenFile("lines", os.O_APPEND|os.O_WRONLY, 0644) if err != nil {<!-- --> fmt.Println(err) return } newLine := "File handling is easy." _, err = fmt.Fprintln(f, newLine) if err != nil {<!-- --> fmt.Println(err) f.Close() return } err = f.Close() if err != nil {<!-- --> fmt.Println(err) return } fmt.Println("file appended successfully") } // File appended successfully
5. Writing to file concurrently Writing to file concurrently
package main import ( "fmt" "math/rand" "os" "sync" ) func produce(data chan int, wg *sync.WaitGroup) {<!-- --> n := rand.Intn(999) // Randomly generate a number within 999 and pass it into the channel data <- n wg.Done() } func consume(data chan int, done chan bool) {<!-- --> f, err := os.Create("concurrent") // Create file if err != nil {<!-- --> fmt.Println(err) return } for d := range data {<!-- --> _, err = fmt.Fprintln(f, d) // Loop into files if err != nil {<!-- --> // If not nil, writing fails fmt.Println(err) f.Close() done <- false // Incoming channel failed return } } err = f.Close() // Close the file if err != nil {<!-- --> fmt.Println(err) done <- false return } done <- true // close channel } func main() {<!-- --> data := make(chan int) done := make(chan bool) wg := sync.WaitGroup{<!-- -->} for i := 0; i < 100; i + + {<!-- --> wg.Add(1) go produce(data, &wg) } go consume(data, done) go func() {<!-- --> wg.Wait() close(data) }() d := <-done if d {<!-- --> fmt.Println("File written successfully") } else {<!-- --> fmt.Println("File writing failed") } } // File written successfully
Please see the previous article for reading files
2. iota constant generator
type WeekDay int const ( Sunday WeekDay = iota Monday Tuesday Wednesday Thursday Friday Saturday ) func main() {<!-- --> fmt.Println(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, WeekDay(2)) fmt.Println(WeekDay(0)) day := WeekDay(1) switch day {<!-- --> case Sunday: fmt.Println("Today is Sunday") case Monday: fmt.Println("Today is Monday") case Tuesday: fmt.Println("Today is Tuesday") case Wednesday: fmt.Println("Today is Wednesday") case Thursday: fmt.Println("Today is Thursday") case Friday: fmt.Println("Today is Friday") case Saturday: fmt.Println("Today is Saturday") default: fmt.Println("Invalid weekday") } } // 0 1 2 3 4 5 6 2 // 0 // Today is Monday
Technical novices record the learning process. If there are any mistakes or confusions, please point them out. If this article is helpful to you, please like and save + follow. Thank you for your support!!!