How to Build a Simple Coffee Shop Application with Go ??
Sai Vivek Nimmathi
SDE @Amazon | Global Rank-119@tcs codevita 2022 | ex - Sonicwall, LTI | Golang | Python | Java | SQL | MongoDB | LLD | HLD | Codeforces | Design Patterns |Micro-services | Distributed systems | Docker | Kubernetes
How to Build a Simple Coffee Shop Application with Go ??
Ever thought about building your own application for a coffee shop? Let’s dive into the basics of using Go to create a simple coffee shop app!
Why Go for Your Coffee Shop App?
Go's efficiency and simplicity make it perfect for building web applications. Let's explore how you can leverage Go to create a basic coffee shop application. ??
1. Setting Up Your Go Environment
First, ensure you have Go installed on your system. If not, download and install it from the official Go website.
Verify the installation by running:
go version
Your terminal should display the installed Go version.
2. Creating a New Go Project
Start by setting up your project structure. Create a new directory for your project:
mkdir coffee-shop
cd coffee-shop
Initialize a new Go module:
go mod init coffee-shop
3. Building a Simple Web Server
Let’s create a basic web server using Go’s net/http package. Create a main.go file and add the following code:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to Go Coffee Shop!")
})
fmt.Println("Server is running on https://localhost:8080")
http.ListenAndServe(":8080", nil)
}
Run the server using:
go run main.go
Open your browser and navigate to https://localhost:8080 to see your running server.
领英推荐
4. Creating a Coffee Menu
Let’s extend the application to display a coffee menu. Modify your main.go file to include a menu handler:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Coffee struct {
Name string `json:"name"`
Price string `json:"price"`
}
func menuHandler(w http.ResponseWriter, r *http.Request) {
menu := []Coffee{
{"Espresso", "$2.00"},
{"Latte", "$2.50"},
{"Cappuccino", "$3.00"},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(menu)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to Go Coffee Shop!")
})
http.HandleFunc("/menu", menuHandler)
fmt.Println("Server is running on https://localhost:8080")
http.ListenAndServe(":8080", nil)
}
Navigate to https://localhost:8080/menu to see your coffee menu in JSON format.
5. Placing an Order
Let's add functionality to place an order. Update your main.go file with an order handler:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Coffee struct {
Name string `json:"name"`
Price string `json:"price"`
}
var orders []Coffee
func menuHandler(w http.ResponseWriter, r *http.Request) {
menu := []Coffee{
{"Espresso", "$2.00"},
{"Latte", "$2.50"},
{"Cappuccino", "$3.00"},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(menu)
}
func orderHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
var order Coffee
json.NewDecoder(r.Body).Decode(&order)
orders = append(orders, order)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(order)
} else {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(orders)
}
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to Go Coffee Shop!")
})
http.HandleFunc("/menu", menuHandler)
http.HandleFunc("/order", orderHandler)
fmt.Println("Server is running on https://localhost:8080")
http.ListenAndServe(":8080", nil)
}
You can now place an order using a POST request to https://localhost:8080/order.
Wrap-Up
Congratulations! You’ve built a basic coffee shop application using Go. This simple app demonstrates how to set up a web server, handle routes, and manage data.
Curious to Learn More?
Check out these resources:
Repost this ?? if you found it helpful and follow me for more Go tutorials and tips!
P.S. What's your favorite coffee? Comment below!