How to solve these coding exercises:
Write your solution in VSCode and then run the program in terminal using: go run main.go
If your solution is not correct, then try to understand the error messages, rewrite the solution and run the program again. Repeat this step until you get the correct solution.
Each exercise should be solved in its own file. Save the file for future reference or recap.
Note: You cannot solve the problem in Go Playground because the program needs access to the local file system (on Go Playground the program is executed on a Google back-end server).
Coding Exercise #1
Create a new file in the current working directory called info.txt and then close the file. If the file cannot be created (no permissions, wrong path etc) then print out the error and stop the program (do error handling).
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #2
Rename the file created at Exercise #1 to data.txt
Check if file exists before renaming it. If it doesn't exist print a message and stop the program.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #3
Remove the file created at exercise #1. Take care that the file is now called data.txt (it has been renamed at exercise #2).
Perform error handling.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #4
Create a Go Program that reads the entire contents of a file called info.txt into a string. You can use ioutil.ReadAll() or any other function you want.
The file exists in the current working directory.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #5
Create a Go Program that reads the entire contents of a file called info.txt using a scanner (bufio package) line by line.
The file exists in the current working directory.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #6
Create a Go Program that:
1. Using single function creates a file called info.txt in the current directory. If the file already exists it will truncate it to zero size.
2. Write the string "The Go gopher is an iconic mascot!" to the file.
Are you stuck? Do you want to see the solution for this exercise? Click here.