Master Building RESTful APIs in Go with this extensive quiz. Learn how to structure your APIs, implement middleware for authentication and logging, and connect with databases using Gorm or SQLx.
MCQs on Building RESTful APIs in Go
Structuring REST APIs in Go
What is the primary purpose of the net/http package in Go when building a RESTful API? a) It provides functions for manipulating files b) It allows you to manage HTTP requests and responses c) It handles JSON serialization d) It is used for routing in web applications
In Go, which function is used to start an HTTP server? a) StartServer() b) ListenAndServe() c) BeginServer() d) RunServer()
What is the role of a router in a Go REST API? a) To handle HTTP requests and route them to appropriate handlers b) To manage the database connections c) To handle authentication d) To process JSON responses
What does the http.HandleFunc() method do in Go? a) It serves static files from a given directory b) It routes an HTTP request to a specific function c) It configures the database connection d) It validates incoming requests
In Go, what is the recommended way to return a JSON response in a REST API handler? a) Use the json.Marshal() function to convert data into JSON format b) Send the data directly as a string c) Return data in XML format d) Use the http.Write() function to send data
How do you define a custom API endpoint in Go? a) By creating a new file b) By writing a function and associating it with a route c) By calling the createEndpoint() function d) By manually setting the HTTP method
What is the purpose of the http.ListenAndServe() function in Go? a) To start the API server and listen for incoming HTTP requests b) To handle database queries c) To start a Goroutine d) To validate incoming requests
Which Go package is commonly used to parse URL parameters in a REST API request? a) encoding/json b) net/http c) github.com/gorilla/mux d) strings
What is a common Go convention for versioning REST APIs? a) Including the version number in the domain name b) Including the version number in the URL path c) Including the version number in the HTTP header d) Using the v query parameter
What does the http.NewServeMux() function do in Go? a) It creates a new HTTP server instance b) It creates a new multiplexer for routing requests c) It handles HTTP responses d) It processes incoming JSON data
Middleware for Authentication and Logging
What is middleware in the context of a Go REST API? a) A database management tool b) A piece of code that processes HTTP requests before they reach the handler c) A function that validates API responses d) A library for serving static files
Which HTTP method is commonly used for authentication in a RESTful API? a) POST b) PUT c) GET d) DELETE
In Go, how do you create a middleware function? a) By defining a function that takes an http.Request and http.ResponseWriter b) By using http.HandleFunc() c) By importing an external middleware library d) By using the mux.NewRouter() function
What is the primary role of authentication middleware in a REST API? a) To check if the user is authorized to access a resource b) To log the API requests c) To handle API errors d) To parse JSON data
Which Go function is commonly used for logging HTTP requests in middleware? a) log.Fatal() b) fmt.Println() c) log.Printf() d) http.Log()
In Go, how do you ensure middleware is applied to an API route? a) By passing the middleware function as an argument to the route handler b) By manually invoking the middleware in each handler function c) By using the http.HandleFunc() function d) By adding the middleware to the main() function
Which of the following is a common method to implement token-based authentication in Go? a) Using a session cookie b) Using an API key passed in the header c) Using JWT tokens in the authorization header d) Using OAuth2 tokens in the query string
How does logging middleware benefit a Go REST API? a) It ensures all requests are logged for debugging and analysis b) It automatically formats HTTP responses c) It limits the rate of requests d) It validates the input data in each request
What is the purpose of the http.Request object in Go’s middleware? a) To store the HTTP response b) To represent the incoming HTTP request c) To manage the database connection d) To manage the router configuration
How do you handle authentication failures in Go middleware? a) By returning a custom error message and an HTTP 401 status code b) By logging the error and continuing the request c) By sending a 500 error code d) By automatically retrying the request
Connecting with Databases using Gorm or SQLx
What is Gorm in Go? a) A library for handling HTTP requests b) A tool for managing user authentication c) A popular ORM (Object-Relational Mapping) library for Go d) A tool for structuring Go APIs
How do you connect to a database using Gorm in Go? a) gorm.Connect() b) gorm.Open() c) database.Connect() d) gorm.Database()
Which Go package is commonly used to work with SQL databases in a more flexible way than the standard database/sql package? a) github.com/gorilla/mux b) github.com/jmoiron/sqlx c) github.com/go-sql-driver/mysql d) github.com/gin-gonic/gin
How do you perform a query with Gorm in Go? a) db.Query() b) db.Select() c) db.Find() d) db.Execute()
Which of the following is used to create database tables with Gorm? a) db.Migrate() b) db.CreateTable() c) db.AutoMigrate() d) db.NewTable()
What is SQLx in Go used for? a) For managing HTTP middleware b) For working with SQL databases more easily than using database/sql c) For logging SQL queries d) For serializing SQL data into JSON format
What is the first step to use Gorm in a Go application? a) Create a new SQL database b) Import the Gorm package c) Write SQL queries d) Create a middleware for authentication
What does Gorm’s AutoMigrate() method do? a) It automatically updates the database schema based on model changes b) It migrates data from one database to another c) It migrates data to a backup server d) It handles user authentication
Which Go package is needed to work with PostgreSQL databases using Gorm? a) github.com/go-sql-driver/mysql b) github.com/lib/pq c) github.com/jmoiron/sqlx d) github.com/gorilla/mux
How do you retrieve data from a database using SQLx in Go? a) db.QueryRow() b) db.Get() c) db.Find() d) db.Select()
Answers Table
Qno
Answer
1
b) It allows you to manage HTTP requests and responses
2
b) ListenAndServe()
3
a) To handle HTTP requests and route them to appropriate handlers
4
b) It routes an HTTP request to a specific function
5
a) Use the json.Marshal() function to convert data into JSON format
6
b) By writing a function and associating it with a route
7
a) To start the API server and listen for incoming HTTP requests
8
c) github.com/gorilla/mux
9
b) Including the version number in the URL path
10
b) It creates a new multiplexer for routing requests
11
b) A piece of code that processes HTTP requests before they reach the handler
12
a) POST
13
a) By defining a function that takes an http.Request and http.ResponseWriter
14
a) To check if the user is authorized to access a resource
15
c) log.Printf()
16
a) By passing the middleware function as an argument to the route handler
17
c) Using JWT tokens in the authorization header
18
a) It ensures all requests are logged for debugging and analysis
19
b) To represent the incoming HTTP request
20
a) By returning a custom error message and an HTTP 401 status code
21
c) A popular ORM (Object-Relational Mapping) library for Go
22
b) gorm.Open()
23
b) github.com/jmoiron/sqlx
24
c) db.Find()
25
c) db.AutoMigrate()
26
b) For working with SQL databases more easily than using database/sql
27
b) Import the Gorm package
28
a) It automatically updates the database schema based on model changes