Ad space (banner)
๐ŸนGo Lessons
Lesson 52 / 68

File Reading and Writing Basics

In this lesson you will learn the basics of reading and writing files in Go, so a program can save and load text files. This is for anyone searching for "Go file read write how to use".

os.WriteFile writes to a file and os.ReadFile reads one back. Saving configuration files and logs makes this a basic operation you will use often.

The sample code saves the contents of a note with os.WriteFile("memo.txt", []byte(...), 0644), then immediately reads the same file back with os.ReadFile("memo.txt") and prints it. Notice that the 0644 is the file's permission bits.

A common stumbling block for beginners is ignoring the error that comes back. Checking it rather than discarding it is the Go habit that makes file handling robust - checking what os.ReadFile returns lets you respond properly when the file is missing.

In real-world development you can also combine file operations with goroutines when you want them to run concurrently.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)