
Practice 50 Go Lang Web Development Coding Questions
Q1. Write a Go program to create a simple web server that listens on port 8080.
Input:
Start the web server on localhost:8080
Expected Output:
Web server is running on localhost:8080
Q2. Write a Go program to handle HTTP GET requests and respond with "Hello, World!".
Input:
HTTP GET request to http://localhost:8080
Expected Output:
"Hello, World!"
Q3. Write a Go program to serve static files from a "static" directory.
Input:
Access http://localhost:8080/static/file.html
Expected Output:
Serve the file.html content from the "static" directory
Q4. Write a Go program to create an HTTP POST handler that accepts JSON data and returns a response.
Input:
POST request to http://localhost:8080 with JSON body: { "name": "John", "age": 25 }
Expected Output:
Received JSON: { "name": "John", "age": 25 }
Q5. Write a Go program to parse URL query parameters and print them.
Input:
Access http://localhost:8080?name=John&age=25
Expected Output:
name: John, age: 25
Q6. Write a Go program to create an HTTP server that handles multiple routes.
Input:
GET request to http://localhost:8080/hello
Expected Output:
"Hello, World!"
Q7. Write a Go program to create a simple login form using HTML and handle its POST request.
Input:
POST request to http://localhost:8080/login with form data: { "username": "admin", "password": "password123" }
Expected Output:
"Login Successful!"
Q8. Write a Go program to handle form submissions and display the submitted data.
Input:
POST request to http://localhost:8080/submit with form data: { "name": "Alice", "age": "30" }
Expected Output:
Submitted data: name: Alice, age: 30
Q9. Write a Go program to create a session-based login system using cookies.
Input:
POST request to http://localhost:8080/login with credentials: { "username": "user", "password": "pass" }
Expected Output:
Session cookie set and user logged in successfully.
Q10. Write a Go program to make an HTTP GET request to an external API and print the response.
Input:
HTTP GET request to https://api.github.com/users/octocat
Expected Output:
{ "login": "octocat", "id": 1, "type": "User", ... }
Q11. Write a Go program to set custom HTTP headers in the response.
Input:
HTTP GET request to http://localhost:8080 with a custom header "X-Custom-Header: 123"
Expected Output:
Response header: X-Custom-Header: 123
Q12. Write a Go program to create a RESTful API with GET and POST methods.
Input:
GET request to http://localhost:8080/api/users
Expected Output:
[{ "id": 1, "name": "John" }]
Q13. Write a Go program to create a simple web page that displays a list of items from an array.
Input:
Access http://localhost:8080/items
Expected Output:
Items: [Item 1, Item 2, Item 3]
Q14. Write a Go program to implement a simple login system with username and password.
Input:
POST request to http://localhost:8080/login with data: { "username": "admin", "password": "admin123" }
Expected Output:
"Login Successful!"
Q15. Write a Go program to render HTML templates with dynamic data.
Input:
Access http://localhost:8080/greet?name=Alice
Expected Output:
"Hello, Alice!"
Q16. Write a Go program to send a JSON response from an HTTP handler.
Input:
GET request to http://localhost:8080/api/data
Expected Output:
{ "name": "John", "age": 25 }
Q17. Write a Go program to connect to a MySQL database and display data on a web page.
Input:
Access http://localhost:8080/users
Expected Output:
List of users: [User 1, User 2, User 3]
Q18. Write a Go program to parse form data submitted through a POST request.
Input:
POST request to http://localhost:8080/register with data: { "email": "john@example.com", "password": "securepass" }
Expected Output:
"Registration Successful!"
Q19. Write a Go program to implement URL routing using the `mux` router.
Input:
GET request to http://localhost:8080/route1
Expected Output:
"You hit route1"
Q20. Write a Go program to create a simple API that returns user data in JSON format.
Input:
GET request to http://localhost:8080/api/user/1
Expected Output:
{ "id": 1, "name": "John Doe" }
Q21. Write a Go program to add CSRF protection to a web form.
Input:
POST request to http://localhost:8080/form with form data and CSRF token
Expected Output:
"CSRF Token is valid, form submitted."
Q22. Write a Go program to create a web page that allows users to upload a file.
Input:
POST request to http://localhost:8080/upload with a file attached
Expected Output:
"File uploaded successfully."
Q23. Write a Go program to display a list of users fetched from a database on a web page.
Input:
Access http://localhost:8080/users
Expected Output:
List of users: [User 1, User 2, User 3]
Q24. Write a Go program to implement user authentication with JWT tokens.
Input:
POST request to http://localhost:8080/login with credentials: { "username": "user", "password": "pass" }
Expected Output:
JWT Token: <generated-token>
Q25. Write a Go program to create a basic shopping cart using sessions and cookies.
Input:
POST request to http://localhost:8080/add-to-cart with item data
Expected Output:
"Item added to cart. Current cart: [Item 1, Item 2]"
Q26. Write a Go program to redirect users to another URL on a specific route.
Input:
Access http://localhost:8080/old-page
Expected Output:
Redirecting to: /new-page
Q27. Write a Go program to handle HTTP PUT requests and update data on the server.
Input:
PUT request to http://localhost:8080/update-user with data: { "id": 1, "name": "John Updated" }
Expected Output:
"User updated successfully."
Q28. Write a Go program to handle HTTP DELETE requests and remove data from the server.
Input:
DELETE request to http://localhost:8080/delete-user/1
Expected Output:
"User with ID 1 deleted."
Q29. Write a Go program to create an HTTP server with middleware that logs each request.
Input:
Access http://localhost:8080/any-page
Expected Output:
Log: Request received for /any-page
Q30. Write a Go program to create a file download route for serving files from the server.
Input:
Access http://localhost:8080/download/file.txt
Expected Output:
"Downloading file.txt"
Q31. Write a Go program to display current server time on a web page.
Input:
Access http://localhost:8080/time
Expected Output:
"Current server time: <current-time>"
Q32. Write a Go program to set up CORS (Cross-Origin Resource Sharing) for a web application.
Input:
HTTP GET request from a different origin
Expected Output:
"CORS headers added to response"
Q33. Write a Go program to create a web page with pagination for displaying a list of users.
Input:
Access http://localhost:8080/users?page=2
Expected Output:
Users: [User 11, User 12, User 13]
Q34. Write a Go program to implement a contact form with validation and email sending functionality.
Input:
POST request to http://localhost:8080/contact with form data: { "name": "Alice", "email": "alice@example.com", "message": "Hello" }
Expected Output:
"Message sent successfully to alice@example.com."
Q35. Write a Go program to create an HTML page with a simple interactive JavaScript button.
Input:
Access http://localhost:8080/button
Expected Output:
"Button clicked: Message displayed"
Q36. Write a Go program to set cookies for a user and display them on a web page.
Input:
Set cookie "user=John" and access http://localhost:8080/cookie
Expected Output:
"User cookie: John"
Q37. Write a Go program to handle HTTP requests with different HTTP methods (GET, POST, PUT, DELETE).
Input:
POST request to http://localhost:8080/create
Expected Output:
"Create request received"
Q38. Write a Go program to display user input on a web page after form submission.
Input:
POST request to http://localhost:8080/submit with data: { "name": "Bob", "age": "35" }
Expected Output:
"You submitted: name: Bob, age: 35"
Q39. Write a Go program to make an asynchronous HTTP request using Go’s goroutines.
Input:
Trigger asynchronous HTTP request
Expected Output:
"Asynchronous request completed"
Q40. Write a Go program to create a dynamic HTML form with fields generated from a database.
Input:
Access http://localhost:8080/form
Expected Output:
"Form with dynamic fields"
Q41. Write a Go program to create a login page with error messages on invalid login attempts.
Input:
POST request to http://localhost:8080/login with invalid credentials: { "username": "wrong", "password": "incorrect" }
Expected Output:
"Invalid username or password."
Q42. Write a Go program to integrate a Go web server with a third-party API like GitHub.
Input:
GET request to http://localhost:8080/github-profile
Expected Output:
"GitHub Profile: {name: 'Octocat', bio: 'GitHub mascot'}"
Q43. Write a Go program to parse a JSON response from an external API and display it on a web page.
Input:
GET request to http://localhost:8080/api/external
Expected Output:
{ "data": "Example" }
Q44. Write a Go program to create a simple search functionality on a web page using user input.
Input:
POST request to http://localhost:8080/search with query: { "searchTerm": "Go" }
Expected Output:
"Search results for: Go"
Q45. Write a Go program to create a REST API endpoint that returns a list of products in JSON format.
Input:
GET request to http://localhost:8080/products
Expected Output:
{ "products": ["Product 1", "Product 2"] }
Q46. Write a Go program to create a basic login form and validate the input using regular expressions.
Input:
POST request to http://localhost:8080/login with data: { "email": "email@domain.com" }
Expected Output:
"Valid email address"
Q47. Write a Go program to implement session management in a web application using cookies.
Input:
Access http://localhost:8080/login and set session cookie
Expected Output:
"Session cookie set for user"
Q48. Write a Go program to implement a simple CRUD (Create, Read, Update, Delete) system with a web interface.
Input:
POST request to http://localhost:8080/create with data: { "item": "item1" }
Expected Output:
"Item added successfully."
Q49. Write a Go program to handle different HTTP status codes and send appropriate responses.
Input:
GET request to http://localhost:8080/notfound
Expected Output:
"404 Not Found"
Q50. Write a Go program to implement a simple real-time chat system using WebSockets.
Input:
WebSocket connection to http://localhost:8080/chat
Expected Output:
"User connected"