大家好,又見面了,我是 GitHub 精選君!
今天要給大家推薦一個 GitHub 開源項目 thangchung/go-coffeeshop,該項目在 GitHub 有超過 3.1k Star,用一句話介紹該項目就是:“☕ A practical event-driven microservices demo built with Golang.”,使用 Golang 開發的事件驅動的微服務示例。
go-coffeeshop 是一個用 Go 語言編寫的開源項目,它模拟了一個咖啡店的線上訂單系統。它使用了 Go 的标準庫以及第三方庫,如 Gin 架構,GORM 資料庫架構和 Redis 緩存資料庫。 這個項目提供了一個簡單的咖啡店訂單系統的模型,示範了如何使用 Go 語言和相關架構來建構 Web 應用程式。它提供了一個可用的 RESTful API,用于訂單的建立,查詢,更新和删除。可以作為 Go 的入門級項目,也可以作為使用 Go 建構 Web 應用程式的參考。
如何安裝使用
因為 go-coffeeshop 包含多個服務,要一步一步運作起來還是比較困難的,是以 go-coffeeshop 使用 docker compose 進行管理,隻需要在倉庫目錄運作 make docker-compose 即可啟動如下服務。
使用示例 DEMO
以下是使用 go-coffeeshop 項目的示例代碼,可以用來建立一個新的訂單:
package main
import (
"fmt"
"net/http"
"bytes"
"io/ioutil"
"encoding/json"
)
func main() {
url := "http://localhost:8080/api/v1/orders"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"coffee_name":"Espresso","size":"small","milk_type":"whole","quantity":1}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
fmt.Println("Order ID: ", result["id"])
}
更多項目詳情請檢視如下連結。
開源項目位址:https://github.com/thangchung/go-coffeeshop
開源項目作者:thangchung
關注我們,一起探索有意思的開源項目。