Loyiha: CRUD
Oldingi darslarda net/http paketining alohida qismlarini o‘rgandik: handler, router, so‘rov va javob, yo‘l hamda query parametrlar. Endi ularni bitta loyihada birlashtiramiz va kitoblar uchun to‘liq ishlaydigan API yozamiz.
CRUD — ma’lumot ustida bajariladigan to‘rtta asosiy amalning qisqartmasi:
| Amal | Ma’nosi | HTTP | Endpoint |
|---|---|---|---|
| Create | Yaratish | POST |
/books |
| Read | O‘qish | GET |
/books, /books/{id} |
| Update | Yangilash | PUT |
/books/{id} |
| Delete | O‘chirish | DELETE |
/books/{id} |
Deyarli har qanday ilovaning asosida CRUD turadi. Blogda maqola yoziladi, o‘qiladi, tahrirlanadi va o‘chiriladi. Do‘konda mahsulot, bankda hisob, kutubxonada kitob — barchasi shu to‘rt amal atrofida quriladi.
Loyiha faqat standart kutubxonadan foydalanadi. Ma’lumotlar hozircha xotirada (map ichida) saqlanadi. Keyingi bo‘limlarda ularni PostgreSQL’ga ko‘chiramiz.
Loyihani tayyorlash
Barcha kod main.go faylida bo‘ladi. Kodni qismlarga bo‘lib tushuntiramiz, to‘liq fayl esa dars oxirida keltirilgan.
1. Model
Avval API qanday ma’lumot bilan ishlashini belgilaymiz:
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Year int `json:"year"`
CreatedAt time.Time `json:"created_at"`
}
type bookInput struct {
Title string `json:"title"`
Author string `json:"author"`
Year int `json:"year"`
}
Book — saqlanadigan va klientga qaytariladigan to‘liq kitob. bookInput — klient yaratish yoki yangilash paytida yuboradigan ma’lumot.
Nima uchun ikkita alohida tur? ID va CreatedAt qiymatlarini server o‘zi belgilaydi. Agar so‘rov tanasi to‘g‘ridan-to‘g‘ri Bookga o‘qilsa, klient "id": 999 yuborib, ID’ni o‘zi tanlab olishi mumkin bo‘lardi. Alohida kirish turi bunday holatning oldini oladi.
2. Ma’lumotlar ombori (store)
Kitoblar map ichida saqlanadi. Server har bir so‘rovni alohida goroutine’da qayta ishlaydi. Shu sababli map’ga murojaat sync.RWMutex bilan himoyalanadi:
type store struct {
mu sync.RWMutex
books map[int]Book
nextID int
}
func newStore() *store {
return &store{books: make(map[int]Book), nextID: 1}
}
RWMutex ikki xil qulf beradi. RLock — o‘qish uchun: bir vaqtda ko‘p goroutine o‘qishi mumkin. Lock — yozish uchun: yozuvchi ishlayotganda boshqa hech kim o‘qiy olmaydi va yoza olmaydi. API’da odatda o‘qish yozishdan ancha ko‘p bo‘ladi, shuning uchun RWMutex mos keladi.
Omborning metodlari:
var errNotFound = errors.New("kitob topilmadi")
func (s *store) list() []Book {
s.mu.RLock()
defer s.mu.RUnlock()
books := make([]Book, 0, len(s.books))
for _, b := range s.books {
books = append(books, b)
}
slices.SortFunc(books, func(a, b Book) int { return a.ID - b.ID })
return books
}
func (s *store) get(id int) (Book, error) {
s.mu.RLock()
defer s.mu.RUnlock()
b, ok := s.books[id]
if !ok {
return Book{}, errNotFound
}
return b, nil
}
func (s *store) create(in bookInput) Book {
s.mu.Lock()
defer s.mu.Unlock()
b := Book{
ID: s.nextID,
Title: in.Title,
Author: in.Author,
Year: in.Year,
CreatedAt: time.Now().UTC(),
}
s.books[b.ID] = b
s.nextID++
return b
}
func (s *store) update(id int, in bookInput) (Book, error) {
s.mu.Lock()
defer s.mu.Unlock()
b, ok := s.books[id]
if !ok {
return Book{}, errNotFound
}
b.Title, b.Author, b.Year = in.Title, in.Author, in.Year
s.books[id] = b
return b, nil
}
func (s *store) delete(id int) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.books[id]; !ok {
return errNotFound
}
delete(s.books, id)
return nil
}
E’tibor bering:
listnatijani ID bo‘yicha saralaydi. Map elementlari tasodifiy tartibda aylanadi, saralanmasa har safar boshqa tartib chiqadi.listbo‘sh holatdanilemas, bo‘sh slice qaytaradi. JSON’danilslicenull, bo‘sh slice esa[]bo‘ladi. Klient uchun[]qulayroq.- Topilmagan holat uchun
errNotFoundxatosi bor. Handler shu xatoga qarab404qaytaradi.
3. Yordamchi funksiyalar
Har bir handler JSON yozadi, xato qaytaradi va ID o‘qiydi. Bir xil kodni takrorlamaslik uchun uchta yordamchi funksiya yozamiz:
func writeJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("JSON yozishda xato: %v", err)
}
}
func writeError(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, map[string]string{"error": msg})
}
func parseID(r *http.Request) (int, error) {
id, err := strconv.Atoi(r.PathValue("id"))
if err != nil || id <= 0 {
return 0, errors.New("id musbat butun son bo‘lishi kerak")
}
return id, nil
}
Kirish ma’lumotini o‘qish va tekshirish uchun yana bitta funksiya:
func decodeBook(w http.ResponseWriter, r *http.Request) (bookInput, error) {
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
var in bookInput
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(&in); err != nil {
return in, errors.New("JSON noto‘g‘ri: " + err.Error())
}
in.Title = strings.TrimSpace(in.Title)
in.Author = strings.TrimSpace(in.Author)
switch {
case in.Title == "":
return in, errors.New("title majburiy")
case in.Author == "":
return in, errors.New("author majburiy")
case in.Year < 0 || in.Year > time.Now().Year():
return in, errors.New("year noto‘g‘ri")
}
return in, nil
}
MaxBytesReadertanani 1 MB bilan cheklaydi.DisallowUnknownFieldsnotanish maydonlarni rad etadi. Masalan,"titel"deb xato yozilgan maydon jimgina e’tiborsiz qolmaydi.TrimSpacebo‘shliqlarni olib tashlaydi. Aks holda" "ham to‘g‘ri sarlavha deb qabul qilinardi.
Bu tekshiruvlar hozircha qo‘lda yozildi. JSON bo‘limida ularni ancha qulayroq usullar bilan almashtiramiz.
4. Handlerlar
Handlerlar storega murojaat qilishi kerak. Buning uchun ularni server struct metodlari sifatida yozamiz. Global o‘zgaruvchi ishlatilmaydi:
Ro‘yxat: GET /books
func (s *server) listBooks(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.store.list())
}
Bitta kitob: GET /books/{id}
func (s *server) getBook(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
b, err := s.store.get(id)
if errors.Is(err, errNotFound) {
writeError(w, http.StatusNotFound, err.Error())
return
}
writeJSON(w, http.StatusOK, b)
}
Yaratish: POST /books
func (s *server) createBook(w http.ResponseWriter, r *http.Request) {
in, err := decodeBook(w, r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
b := s.store.create(in)
w.Header().Set("Location", fmt.Sprintf("/books/%d", b.ID))
writeJSON(w, http.StatusCreated, b)
}
Yangi resurs yaratilganda 201 Created qaytariladi. Location sarlavhasi yangi kitob manzilini bildiradi.
Yangilash: PUT /books/{id}
func (s *server) updateBook(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
in, err := decodeBook(w, r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
b, err := s.store.update(id, in)
if errors.Is(err, errNotFound) {
writeError(w, http.StatusNotFound, err.Error())
return
}
writeJSON(w, http.StatusOK, b)
}
PUT resursni to‘liq almashtiradi, shuning uchun barcha maydonlar majburiy. Faqat bitta maydonni o‘zgartirish kerak bo‘lsa, PATCH ishlatiladi. Uni loyihani oddiyroq saqlash uchun bu yerda yozmadik.
O‘chirish: DELETE /books/{id}
func (s *server) deleteBook(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if err := s.store.delete(id); errors.Is(err, errNotFound) {
writeError(w, http.StatusNotFound, err.Error())
return
}
w.WriteHeader(http.StatusNoContent)
}
Muvaffaqiyatli o‘chirishdan keyin 204 No Content qaytariladi. Javobda tana bo‘lmaydi.
5. Router va server
func (s *server) routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /books", s.listBooks)
mux.HandleFunc("POST /books", s.createBook)
mux.HandleFunc("GET /books/{id}", s.getBook)
mux.HandleFunc("PUT /books/{id}", s.updateBook)
mux.HandleFunc("DELETE /books/{id}", s.deleteBook)
return mux
}
s.listBooks — metod qiymati (method value). U s obyektiga bog‘langan oddiy funksiyaga aylanadi va HandleFuncga to‘g‘ridan-to‘g‘ri beriladi.
To‘liq kod
| main.go | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
API’ni sinab ko‘rish
Serverni ishga tushiring:
Boshqa terminalda curl bilan har bir amalni tekshiramiz.
Kitob yaratish:
curl -i -X POST http://localhost:8080/books \
-H "Content-Type: application/json" \
-d '{"title":"O‘tkan kunlar","author":"Abdulla Qodiriy","year":1926}'
HTTP/1.1 201 Created
Content-Type: application/json
Location: /books/1
{"id":1,"title":"O‘tkan kunlar","author":"Abdulla Qodiriy","year":1926,"created_at":"2026-09-30T10:00:00.123456Z"}
Yana bitta kitob qo‘shamiz:
curl -X POST http://localhost:8080/books \
-H "Content-Type: application/json" \
-d '{"title":"Kecha va kunduz","author":"Cho‘lpon","year":1936}'
Ro‘yxatni olish:
[{"id":1,"title":"O‘tkan kunlar","author":"Abdulla Qodiriy","year":1926,"created_at":"2026-09-30T10:00:00.123456Z"},{"id":2,"title":"Kecha va kunduz","author":"Cho‘lpon","year":1936,"created_at":"2026-09-30T10:00:05.654321Z"}]
Yangilash:
curl -X PUT http://localhost:8080/books/2 \
-H "Content-Type: application/json" \
-d '{"title":"Kecha va kunduz","author":"Abdulhamid Cho‘lpon","year":1936}'
O‘chirish:
Xato holatlar:
curl http://localhost:8080/books/1
curl http://localhost:8080/books/abc
curl -X POST http://localhost:8080/books -d '{"title":""}'
curl -X POST http://localhost:8080/books -d '{"titel":"Sarob"}'
{"error":"kitob topilmadi"}
{"error":"id musbat butun son bo‘lishi kerak"}
{"error":"title majburiy"}
{"error":"JSON noto‘g‘ri: json: unknown field \"titel\""}
Info
Ma’lumotlar xotirada saqlanadi. Server qayta ishga tushirilsa, barcha kitoblar yo‘qoladi. Buni Ma’lumotlar bazasi bo‘limida PostgreSQL yordamida hal qilamiz.
Nimani yaxshilash mumkin?
Loyiha ishlaydi, lekin real ilova uchun hali ko‘p narsa yetishmaydi. Keyingi darslarda ularni bosqichma-bosqich qo‘shamiz:
- har bir so‘rovni log qilish va xatolarni bir joyda ushlash — middleware;
- kodni
handler,service,repositoryqatlamlariga ajratish — clean architecture; - tekshiruvlarni
validatorkutubxonasi bilan yozish va xatolarni yagona formatda qaytarish — JSON bo‘limi; - ma’lumotlarni PostgreSQL’da saqlash — ma’lumotlar bazasi bo‘limi;
- foydalanuvchilarni tanish va ruxsatlarni tekshirish — authentication va authorization.
Xulosa
Bu loyihada:
- CRUD amallarini REST endpointlariga moslashtirdik;
- ichki model (
Book) va kirish ma’lumotini (bookInput) ajratdik; - umumiy ma’lumotni
sync.RWMutexbilan himoyaladik; writeJSON,writeError,parseIDkabi yordamchilar bilan takrorlanishni kamaytirdik;- har bir holat uchun to‘g‘ri status kod qaytardik:
200,201,204,400,404.
Keyingi darsda middleware tushunchasi bilan tanishamiz va barcha so‘rovlarni log qilishni o‘rganamiz.