
Practice 50 Go Lang Popular Libraries and Tools Coding Questions
Q1. Write a Go program that uses the `fmt` package to print a formatted string.
Input:
Name: "John", Age: 30
Expected Output:
Name: John, Age: 30
Q2. Write a Go program to parse a date using the `time` package.
Input:
Date string: "2025-01-14"
Expected Output:
Parsed Date: 2025-01-14 00:00:00 +0000 UTC
Q3. Write a Go program to read a file using the `os` package.
Input:
File path: "example.txt"
Expected Output:
File content: This is an example file.
Q4. Write a Go program to create a HTTP server using the `net/http` package.
Input:
Start HTTP server on port 8080.
Expected Output:
Server running on http://localhost:8080
Q5. Write a Go program using the `math/rand` package to generate a random number.
Input:
Generate a random number between 1 and 100.
Expected Output:
Random number: 42
Q6. Write a Go program using the `strings` package to split a string by space.
Input:
String: "Hello World Go"
Expected Output:
Split strings: ["Hello", "World", "Go"]
Q7. Write a Go program to encode and decode data using the `encoding/json` package.
Input:
Struct: {Name: "Alice", Age: 25}
Expected Output:
JSON: {"Name":"Alice","Age":25}
Decoded: {Name: Alice, Age: 25}
Q8. Write a Go program using the `log` package to log a message.
Input:
Log message: "Server started"
Expected Output:
2025/01/14 10:00:00 Server started
Q9. Write a Go program using the `bufio` package to read user input.
Input:
User Input: "Go is awesome"
Expected Output:
User Input: Go is awesome
Q10. Write a Go program using the `math` package to calculate the square root of a number.
Input:
Number: 16
Expected Output:
Square root: 4
Q11. Write a Go program using the `os/exec` package to execute a shell command.
Input:
Command: "ls"
Expected Output:
File1.txt
File2.txt
Q12. Write a Go program to create and write to a file using the `ioutil` package.
Input:
File path: "output.txt", Content: "This is a test."
Expected Output:
File written successfully.
Q13. Write a Go program using the `net/url` package to parse a URL.
Input:
URL: "https://www.example.com/path?name=John&age=30"
Expected Output:
Parsed URL: scheme=https, host=www.example.com, path=/path, query=name=John&age=30
Q14. Write a Go program using the `sync` package to create a simple Mutex for synchronization.
Input:
Lock and unlock a Mutex.
Expected Output:
Mutex locked and unlocked successfully.
Q15. Write a Go program using the `time` package to get the current timestamp.
Input:
Get current timestamp.
Expected Output:
Current time: 2025-01-14 10:00:00
Q16. Write a Go program using the `reflect` package to get the type of a variable.
Input:
Variable: 123
Expected Output:
Type: int
Q17. Write a Go program using the `strconv` package to convert a string to an integer.
Input:
String: "123"
Expected Output:
Converted Integer: 123
Q18. Write a Go program using the `http` package to make a GET request.
Input:
URL: "http://example.com"
Expected Output:
HTTP Status: 200 OK
Q19. Write a Go program using the `testing` package to write a simple unit test.
Input:
Function to test: Add(a, b int) -> a + b
Expected Output:
Test passed: Add(2, 3) = 5
Q20. Write a Go program using the `flag` package to parse command line arguments.
Input:
Command: go run program.go -name=John
Expected Output:
Name: John
Q21. Write a Go program using the `http` package to create a POST request with JSON data.
Input:
URL: "http://example.com/api", Data: {"name": "John", "age": 30}
Expected Output:
HTTP Status: 200 OK, Response: {"status": "success"}
Q22. Write a Go program using the `encoding/csv` package to read a CSV file.
Input:
CSV file: "data.csv" with contents: Name, Age
John, 25
Expected Output:
CSV data: John, 25
Q23. Write a Go program using the `crypto/aes` package to encrypt a message.
Input:
Message: "Sensitive Data", Key: "key123"
Expected Output:
Encrypted message: [encrypted data]
Q24. Write a Go program using the `math/big` package to perform large number calculations.
Input:
Calculate 1234567890 * 9876543210
Expected Output:
Result: 12193263111263526900
Q25. Write a Go program using the `log` package to log an error message.
Input:
Error: "File not found"
Expected Output:
2025/01/14 10:00:00 File not found
Q26. Write a Go program using the `os` package to check if a file exists.
Input:
File: "data.txt"
Expected Output:
File exists: true
Q27. Write a Go program using the `compress/gzip` package to compress a string.
Input:
String: "Hello, this is a test."
Expected Output:
Compressed string: [compressed data]
Q28. Write a Go program using the `crypto/sha256` package to generate a hash of a string.
Input:
String: "GoLang Security"
Expected Output:
SHA-256 Hash: [hashed value]
Q29. Write a Go program using the `math` package to compute the power of a number.
Input:
Base: 2, Exponent: 3
Expected Output:
Power: 8
Q30. Write a Go program using the `encoding/base64` package to encode and decode data.
Input:
String: "Hello, World!"
Expected Output:
Base64 Encoded: SGVsbG8sIFdvcmxkIQ==
Base64 Decoded: Hello, World!
Q31. Write a Go program using the `time` package to measure the time taken to execute a function.
Input:
Measure execution time of a function.
Expected Output:
Execution time: 1.2345ms
Q32. Write a Go program using the `encoding/xml` package to encode data into XML format.
Input:
Struct: {Name: "John", Age: 30}
Expected Output:
XML: <Person><Name>John</Name><Age>30</Age></Person>
Q33. Write a Go program using the `time` package to format a date.
Input:
Date: 2025-01-14
Expected Output:
Formatted Date: 14-Jan-2025
Q34. Write a Go program using the `io/ioutil` package to read the contents of a file.
Input:
File path: "text.txt"
Expected Output:
File contents: Hello GoLang!
Q35. Write a Go program using the `math/rand` package to generate a random floating-point number.
Input:
Generate a random number between 0.0 and 1.0
Expected Output:
Random number: 0.5342
Q36. Write a Go program using the `strings` package to count occurrences of a substring.
Input:
String: "hello hello hello", Substring: "hello"
Expected Output:
Count: 3
Q37. Write a Go program using the `os` package to remove a file.
Input:
File path: "example.txt"
Expected Output:
File removed successfully.
Q38. Write a Go program using the `sync` package to use a WaitGroup.
Input:
Execute 3 concurrent tasks.
Expected Output:
All tasks completed.
Q39. Write a Go program using the `math` package to compute the factorial of a number.
Input:
Number: 5
Expected Output:
Factorial: 120
Q40. Write a Go program using the `crypto/md5` package to generate an MD5 hash.
Input:
String: "GoLang"
Expected Output:
MD5 Hash: 8c56a55f3b123adf3f0b524ef9063c15
Q41. Write a Go program using the `regexp` package to match a regular expression.
Input:
String: "hello world", Pattern: "^hello"
Expected Output:
Match found: hello
Q42. Write a Go program using the `http` package to handle a POST request with form data.
Input:
Form data: "name=John&age=30"
Expected Output:
Form submitted successfully.
Q43. Write a Go program using the `strconv` package to convert an integer to a string.
Input:
Integer: 123
Expected Output:
String: "123"
Q44. Write a Go program using the `encoding/gob` package to encode and decode data.
Input:
Struct: {Name: "Alice", Age: 25}
Expected Output:
Encoded: [encoded data]
Decoded: {Name: Alice, Age: 25}
Q45. Write a Go program using the `log` package to log a warning message.
Input:
Warning: "Low disk space"
Expected Output:
2025/01/14 10:00:00 Low disk space
Q46. Write a Go program using the `math/rand` package to shuffle a slice.
Input:
Slice: [1, 2, 3, 4, 5]
Expected Output:
Shuffled Slice: [4, 2, 1, 5, 3]
Q47. Write a Go program using the `encoding/json` package to pretty-print a JSON object.
Input:
JSON: {"name": "Alice", "age": 25}
Expected Output:
Pretty JSON:
{
"name": "Alice",
"age": 25
}
Q48. Write a Go program using the `os` package to get the current working directory.
Input:
Get current working directory.
Expected Output:
Current Directory: /home/user/projects/go
Q49. Write a Go program using the `fmt` package to format output with custom width and alignment.
Input:
Format: "%-10s %10d"
String: "John", Integer: 25
Expected Output:
John 25
Q50. Write a Go program using the `time` package to calculate the difference between two dates.
Input:
Start Date: "2025-01-01", End Date: "2025-01-14"
Expected Output:
Date Difference: 13 days