How to read and write files in Go: a complete guide for beginners
Hi everyone! I just started learning Go and encountered a problem when working with files. I need to write a simple program that reads data from one text file, processes it (for example, converts it to uppercase) and writes the result to another file.\n\nI tried several approaches but keep getting errors. Here is my current code:\n\n```go\npackage main\n\nimport (\n "fmt"\n "os"\n)\n\nfunc main() {\n // Trying to read a file\n data, err := os.Read("input.txt")\n if err != nil {\n fmt.Println("Read error:", err)\n return\n }\n \n // Convert to string and try to write\n err = os.Write("output.txt", data)\n if err != nil {\n fmt.Println("Write error:", err)\n return\n }\n \n fmt.Println("Done")
No answers yet.
Log in to leave an answer