To append data to an existing file in Go, we first need to open the file in append mode. We can do that using the os.OpenFile() function with the appropriate flags. Once we have the file descriptor, we can use the io.WriteString() function to write the new data to the end of the file. Here's an example:
main.go411 chars22 lines
In the code sample above, we open the file "data.txt" in append mode, creating it if it doesn't exist. We then write the new data "new data\n" to the end of the file using the io.WriteString() function. Finally, we close the file descriptor using defer to ensure it gets closed when the function exits.
gistlibby LogSnag