Membaca semua data di struct dengan Gofiber
package main
import (
"github.com/gofiber/fiber/v2"
)
// Struct Product
type Product struct {
Name string `json:"name"`
Price float64 `json:"price"`
Stock int `json:"stock"`
}
func main() {
// Inisialisasi objek Fiber
app := fiber.New()
products := []Product{
{"Product A", 10.99, 100},
{"Product B", 20.50, 50},
{"Product C", 15.75, 75},
}
// Route untuk menampilkan data produk
app.Get("/products", func(c *fiber.Ctx) error {
// Kirim data produk dalam format JSON
return c.JSON(products)
})
// Jalankan server di port tertentu
app.Listen(":3000")
}Last updated