Sobes.tech
Junior — Middle

Have you applied the REST architectural style when developing your services?

sobes.tech AI

Answer from AI

Yes, when developing services in Go, I used the REST architectural style. This involved creating an API where each resource is represented by a unique URL, and operations on them correspond to HTTP methods: GET for retrieving data, POST for creating, PUT/PATCH for updating, and DELETE for deleting. This approach simplifies interaction between client and server and ensures scalability.

An example of a simple REST endpoint in Go using net/http:

package main

import (
    "encoding/json"
    "net/http"
)

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

func getUser(w http.ResponseWriter, r *http.Request) {
    user := User{ID: 1, Name: "Ivan"}
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(user)
}

func main() {
    http.HandleFunc("/user", getUser)
    http.ListenAndServe(":8080", nil)
}

In this example, a GET request to /user returns a JSON with user data.