How Golang will solve your first working task

Maksim Tsvetkov - Jul 2 - - Dev Community

Golang, or just Go, has become known as the Go gopher language. If you still don't understand why the gopher you can find a surprising mascot history in this article. Well, let's get it started from the beginning, Golang has become known as one of the most productive programming languages out there. Unlike traditional languages like Java or Python, Go strikes a unique balance between easy-to-write code and quick execution, speeding up development and cutting down on debugging and testing time. This article will look at a real-world example of using goroutines and context in a project that interacts with a slow and unstable REST API microservice.

The Task
Let's imagine you have just joined the team and your team lead asks you to bind a new microservice written in Golang (for example the risk manager) to the user microservice exposed through REST API.

The Problem
The risk manager needs to interact with a REST API that can be slow and unstable, requiring us to handle such requests carefully. I will use goroutines for asynchronous HTTP requests and context to manage request timeouts and error handling.

The solution

Using goroutines and context in Golang allows efficient management of concurrent tasks and handling of slow or unstable external APIs.

The first step to interpreting it in a code is to create the API, I used the shareware service https://mockapi.io/ It is convenient to generate a REST API with a basic set of entities, such as users.

Image description

Let’s say someone tried their best and your company has an internal service that lists users. Our task is to reuse the user list in the new risk manager (the user data structure satisfies the contract described by mockapi.io). The code below makes a request, processes the body, and produces either a list of users or the corresponding error.

type User struct {
    CreatedAt string
    Name      string
    Avatar    string
    Id        string
}

// Original slow request
func fetchUsers(ctx context.Context) (*[]User, error) {
    resp, err := http.Get("https://<use your id after sign up>.mockapi.io/api/v1/users")
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("failed to fetch users: %s", resp.Status)
    }

    var users []User
    if err := json.NewDecoder(resp.Body).Decode(&users); err != nil {
        return nil, err
    }

    return &users, nil
}
Enter fullscreen mode Exit fullscreen mode

To control the operation, I will use the topmost parent context for all other contexts - context.Background. I will supplement this context with new data, in this case the timeout - context.WithTimeout, which I will define as 2 seconds. You can read more about working with contexts in Go in this article.

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

usersChan := make(chan *[]User)
errChan := make(chan error)

go func() {
    users, err := fetchUsers(ctx)
    if err != nil {
        errChan <- err
        return
    }
    usersChan <- user
}()

select {
case users := <-usersChan:
    fmt.Printf("Fetched users: %+v\n", users)
case err := <-errChan:
    fmt.Printf("Error fetching users: %v\n", err)
case <-ctx.Done():
    fmt.Println("Request timed out")
}
Enter fullscreen mode Exit fullscreen mode

In the example, I use two channels - usersChan to record the result received from our internal service, and the second channel errChan - to record the error.
If the response from a slow API does not arrive within 2 seconds, we detect an error and give the opportunity to correctly process it on an external layer.

In this practical example with the risk manager, goroutines enabled asynchronous HTTP requests, while context ensured timeout handling, which is critical for building reliable and resilient microservices.

P.S.

This article is intended for beginner developers who have basic knowledge, but little idea of ​​where they can apply their knowledge. I will be glad to receive your feedback and will be happy to answer your questions.
A working example and several other implementations of the most basic things in the Go language can be found on my GitHub, at the link

.