package main import ( "os" ) func main() { file, err := os.Create("file.txt") if err != nil { panic(err) } defer file.Close() _, err = file.WriteString("Hello, World!") if err != nil { panic(err) } }
package main import ( "os" ) func main() { file, err := os.OpenFile("file.txt", os.O_WRONLY|os.O_APPEND, 0644) if err != nil { panic(err) } defer file.Close() _, err = file.WriteString("Goodbye!") if err != nil { panic(err) } }In the first example, we used the `os.Create` function to create a new file named `file.txt` in write mode. We then wrote the string "Hello, World!" to the file using the `WriteString` function. Finally, we closed the file using the `Close` method. In the second example, we used the `os.OpenFile` function to open an existing file named `file.txt` in append mode. We then appended the string "Goodbye!" to the end of the file using the `WriteString` function. Finally, we closed the file using the `Close` method. The `os` package is the package library used in these examples to provide the `WriteString` function.