WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Expert · question 75 of 100

How do you design and implement APIs in Go?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Designing and implementing APIs in Go involves a few essential steps, such as defining the API structure, choosing an HTTP framework, handling requests and responses, testing the API, and applying best practices. Let’s dive into the details.

1. Define the API structure

First, you need to plan your API’s resources, endpoints, and the supported CRUD operations. Determine the desired JSON or XML format for request payloads and responses as well.

For this example, let’s assume we are building a simple API to manage Books with the following structure:

- ‘GET /books‘: List all the books

- ‘POST /books‘: Add a new book

- ‘GET /books/id‘: Get a specific book by ID

- ‘PUT /books/id‘: Update a specific book by ID

- ‘DELETE /books/id‘: Delete a specific book by ID

2. Choose an HTTP framework

For a basic API, Go’s built-in ‘net/http‘ package is sufficient. However, for more complex cases, you may consider using a third-party framework like Gin or Echo, which offer additional features like middleware, route groups, and easier validation.

In this example, we will use the standard ‘net/http‘ package.

3. Implement the API

Now, let’s implement the API. First, create a ‘Book‘ struct:

type Book struct {
    ID     string  `json:"id"`
    Title  string  `json:"title"`
    Author string  `json:"author"`
    ISBN   string  `json:"isbn"`
}

Now let’s create the necessary functions for each endpoint:

// Function to handle the "GET /books" endpoint
func getBooks(w http.ResponseWriter, req *http.Request) {
    // ...Implement logic to fetch and return all books...
}

// Function to handle the "POST /books" endpoint
func addBook(w http.ResponseWriter, req *http.Request) {
    // ...Implement logic to create a new book...
}

// Function to handle the "GET /books/{id}" endpoint
func getBook(w http.ResponseWriter, req *http.Request) {
    // ...Implement logic to fetch and return a specific book...
}

// Function to handle the "PUT /books/{id}" endpoint.
func updateBook(w http.ResponseWriter, req *http.Request) {
    // ...Implement logic to update a specific book...
}

// Function to handle the "DELETE /books/{id}" endpoint
func deleteBook(w http.ResponseWriter, req *http.Request) {
    // ...Implement logic to delete a specific book...
}

Now, let’s create a router and define the routes:

func main() {
    router := http.NewServeMux()

    router.HandleFunc("/books", getBooks)
    router.HandleFunc("/books", addBook)
    router.HandleFunc("/books/{id}", getBook)
    router.HandleFunc("/books/{id}", updateBook)
    router.HandleFunc("/books/{id}", deleteBook)

    http.ListenAndServe(":8000", router)
}

4. Test the API

You can test the API using tools like Postman, curl, or by writing automated tests with Go’s ‘testing‘ package. While testing, make sure to verify the correctness of request payloads, response payloads, status codes, and headers.

5. Best practices

- Use proper HTTP status codes to indicate the result of the API operation.

- Add input validation, ensuring that incoming payloads are valid and complete.

- Use middleware for tasks such as logging, authentication, and rate-limiting.

- Adopt the principles of RESTful design, including stateless endpoints and a clear separation of resources.

- Consider versioning your API to manage changes without breaking existing clients.

- Write documentation to help users understand how to use your API.

By following the above steps and adhering to best practices, you can design and implement robust, efficient, and maintainable APIs in Go.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics