
Practice 50 Go Lang File Handling and I/O Coding Questions
Q1. Write a Go program to create a file and write "Hello, Go!" into it.
Input:
File name: hello.txt
Content: Hello, Go!
Expected Output:
File hello.txt created with content: Hello, Go!
Q2. Write a Go program to read the content of a text file and print it to the console.
Input:
File: hello.txt (contains: "Hello, Go!")
Expected Output:
Hello, Go!
Q3. Write a Go program to append text to an existing file.
Input:
Append: "This is Go programming."
File: hello.txt
Expected Output:
File hello.txt updated with content: Hello, Go! This is Go programming.
Q4. Write a Go program to create a new file and write multiple lines of text to it.
Input:
Line 1: Hello
Line 2: World
Line 3: Go is great!
Expected Output:
File created with content:
Hello
World
Go is great!
Q5. Write a Go program to check if a file exists and print a message accordingly.
Input:
File: non_existent_file.txt
Expected Output:
File does not exist
Q6. Write a Go program to copy the content of one file to another.
Input:
Source file: source.txt
Destination file: destination.txt
Expected Output:
Content from source.txt copied to destination.txt
Q7. Write a Go program to count the number of lines in a file.
Input:
File: sample.txt (contains: "Hello\nGo\nProgramming")
Expected Output:
Number of lines: 3
Q8. Write a Go program to read a file line by line and print each line.
Input:
File: sample.txt (contains: "Line 1\nLine 2\nLine 3")
Expected Output:
Line 1
Line 2
Line 3
Q9. Write a Go program to read a file and count the occurrences of a specific word.
Input:
File: text.txt (contains: "Go Go Go Programming")
Word: Go
Expected Output:
Word 'Go' occurs 3 times in the file
Q10. Write a Go program to read a file and print only the first 10 characters.
Input:
File: hello.txt (contains: "Hello, Go programming!")
Expected Output:
First 10 characters: Hello, Go
Q11. Write a Go program to write numbers from 1 to 10 to a file, each on a new line.
Input:
Numbers: 1, 2, 3, ..., 10
Expected Output:
File content:
1
2
3
4
5
6
7
8
9
10
Q12. Write a Go program to write a struct to a file using JSON encoding.
Input:
Struct: { Name: "Alice", Age: 30 }
Expected Output:
File content: {"Name":"Alice","Age":30}
Q13. Write a Go program to read data from a file and print the number of words.
Input:
File: sample.txt (contains: "Hello Go World")
Expected Output:
Word count: 3
Q14. Write a Go program to write a string to a file in binary format.
Input:
String: "Go programming"
Expected Output:
Binary file created with the string data
Q15. Write a Go program to open a file and write text in it if it is not empty.
Input:
File: test.txt (contains: "Hello")
Expected Output:
File is not empty, appending text.
Q16. Write a Go program to read a CSV file and print each record.
Input:
CSV: Name, Age
Alice, 30
Bob, 25
Expected Output:
Name: Alice, Age: 30
Name: Bob, Age: 25
Q17. Write a Go program to create a temporary file and write data to it.
Input:
Temporary File content: "Temporary data"
Expected Output:
Temporary file created with content: Temporary data
Q18. Write a Go program to read a file byte by byte and print each byte.
Input:
File: hello.txt (contains: "Go")
Expected Output:
Byte 1: 71
Byte 2: 111
Q19. Write a Go program to read a file and print its size.
Input:
File: hello.txt (size: 15 bytes)
Expected Output:
File size: 15 bytes
Q20. Write a Go program to read a file using ioutil.ReadFile and print the content.
Input:
File: content.txt (contains: "Go is awesome!")
Expected Output:
Go is awesome!
Q21. Write a Go program to open a file, read its content, and handle errors if the file does not exist.
Input:
File: non_existent_file.txt
Expected Output:
Error: File does not exist
Q22. Write a Go program to write a file with newline characters.
Input:
Content: "First line\nSecond line\nThird line"
Expected Output:
File content:
First line
Second line
Third line
Q23. Write a Go program to open a file in write-only mode and write text to it.
Input:
Text: "Write-only mode"
Expected Output:
File written successfully
Q24. Write a Go program to write a list of numbers to a file and then read them back.
Input:
Numbers: 1, 2, 3, 4, 5
Expected Output:
File content: 1, 2, 3, 4, 5
Read content: 1, 2, 3, 4, 5
Q25. Write a Go program to write a floating-point number to a file.
Input:
Number: 3.14
Expected Output:
File content: 3.14
Q26. Write a Go program to read and print the first word from a file.
Input:
File: sample.txt (contains: "Go programming is fun")
Expected Output:
First word: Go
Q27. Write a Go program to read a text file and store its content into a string variable.
Input:
File: content.txt (contains: "File content example")
Expected Output:
File content: File content example
Q28. Write a Go program to read a file in chunks of 10 bytes and print each chunk.
Input:
File: hello.txt (contains: "Go programming language")
Expected Output:
Chunk 1: Go program
Chunk 2: ming langu
Chunk 3: age
Q29. Write a Go program to remove a file after reading its content.
Input:
File: temp.txt (contains: "Temporary file")
Expected Output:
File temp.txt deleted
Q30. Write a Go program to read a file and print the content in reverse order.
Input:
File: sample.txt (contains: "Hello Go World")
Expected Output:
World Go Hello
Q31. Write a Go program to create a new file and write a formatted string to it.
Input:
File: formatted.txt
Content: "Name: %s, Age: %d"
Name: Alice, Age: 25
Expected Output:
File formatted.txt created with content: Name: Alice, Age: 25
Q32. Write a Go program to read a CSV file, parse the data, and store it in a map.
Input:
CSV: Name, Age
Alice, 30
Bob, 25
Expected Output:
Data stored in map: {Alice: 30, Bob: 25}
Q33. Write a Go program to open a file, read data in binary format, and display it as a string.
Input:
File: binarydata.bin (contains binary data of the string "Go")
Expected Output:
Read binary data as string: Go
Q34. Write a Go program to create a new file and write an integer array to it.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
File content: 1 2 3 4 5
Q35. Write a Go program to read a file and print every third character.
Input:
File: sample.txt (contains: "ABCDEFGHI")
Expected Output:
Characters: C F I
Q36. Write a Go program to create a log file and write timestamped logs to it.
Input:
Log message: "Process started"
Timestamp: Current time
Expected Output:
Log file created with entry: [timestamp] Process started
Q37. Write a Go program to read data from a file, split it into words, and print the words.
Input:
File: sentence.txt (contains: "Go is amazing")
Expected Output:
Words: Go, is, amazing
Q38. Write a Go program to check if a file is empty and print a message.
Input:
File: empty.txt
Expected Output:
File is empty
Q39. Write a Go program to read a file in a loop and print each line, stopping after 5 lines.
Input:
File: multiline.txt (contains 10 lines of text)
Expected Output:
Line 1: Text 1
Line 2: Text 2
Line 3: Text 3
Line 4: Text 4
Line 5: Text 5
Q40. Write a Go program to read a JSON file and unmarshal it into a struct.
Input:
File: data.json (contains: {"Name": "Alice", "Age": 25})
Struct: {Name: string, Age: int}
Expected Output:
Struct: {Name: Alice, Age: 25}
Q41. Write a Go program to read a file and write each word to a new line in another file.
Input:
File: sentence.txt (contains: "Go is great")
Expected Output:
File output:
Go
is
great
Q42. Write a Go program to update a file by replacing a specific word with another word.
Input:
File: text.txt (contains: "Go is fun")
Replace "fun" with "awesome"
Expected Output:
File updated: Go is awesome
Q43. Write a Go program to write the square of numbers 1 to 5 in a file.
Input:
Numbers: 1, 2, 3, 4, 5
Output: 1, 4, 9, 16, 25
Expected Output:
File content: 1 4 9 16 25
Q44. Write a Go program to write user input to a file.
Input:
User input: "Hello Go!"
Expected Output:
File created with content: Hello Go!
Q45. Write a Go program to read a file and print each character's ASCII value.
Input:
File: hello.txt (contains: "Hi")
Expected Output:
Character H: ASCII 72
Character i: ASCII 105
Q46. Write a Go program to read a file line by line and count the number of characters in each line.
Input:
File: lines.txt (contains: "Go\nProgramming\nLanguage")
Expected Output:
Line 1: 2 characters
Line 2: 11 characters
Line 3: 8 characters
Q47. Write a Go program to read a file and print lines containing a specific keyword.
Input:
File: log.txt (contains: "Error: Something went wrong\nInfo: Process started\nError: File not found")
Keyword: "Error"
Expected Output:
Error: Something went wrong
Error: File not found
Q48. Write a Go program to write binary data to a file and then read it back.
Input:
Data: byte slice [0x01, 0x02, 0x03]
Expected Output:
File content in binary: 01 02 03
Q49. Write a Go program to create a file and write a large amount of data to it.
Input:
Data: 1 million characters "A"
Expected Output:
File created with 1 million characters.
Q50. Write a Go program to rename a file and print a success message.
Input:
Old file: old_name.txt
New file: new_name.txt
Expected Output:
File renamed from old_name.txt to new_name.txt