๐ On the Hashnode platform, welcome to yet another fascinating technical instruction! In this fascinating voyage, we'll explore the world of the Go programming language (Golang) and build an alluring application for buying tickets. You'll be the happy owner of a completely functional ticket booking app by the conclusion of this wonderful tutorial, and you'll be shouting "Booked!" with excitement.
Prerequisites ๐
Before we dive into the code, make sure you have Go installed on your machine. Simply head over to the official Go website golang.org and grab the latest version of Go.
Setting Up the Project ๐ ๏ธ
To kickstart our coding adventure, let's create a new project folder and navigate to it in your terminal. Our project will consist of three code files: main.go
, helper.go
, and go.mod
.
The main.go
file will be our main playground where all the magic happens. Here's a sneak peek at what it looks like:
// main.go
package main
import (
"fmt"
"strings"
)
const ConferenceTickets int = 50
var ConferenceName = "Go Conference"
var remainingTickets uint = 50
var bookings = []string{}
// Exciting code snippets ahead! Let's buckle up!
In this code snippet, we set up some variables and constants that will be the backbone of our app. remainingTickets
will keep track of the available tickets, while bookings
will hold the names of the folks who managed to secure their spots at the conference. ๐
Greeting Users ๐
Let's write a function called greetUsers() that will greet the users and provide them with juicy conference details. The code is:
// main.go
// Rest of the code...
func greetUsers() {
fmt.Printf("Welcome to %v booking application\n", ConferenceName)
fmt.Printf("We have a total of %v tickets, and %v are still available\n", ConferenceTickets, remainingTickets)
fmt.Println("Get your tickets here")
}
// Rest of the code...
Getting User Input
Next, we need to gather some information from the users, such as their names, email addresses, and the number of tickets they want to book. We'll create a function called getUserInput()
to handle this task:
// main.go
// Rest of the code...
func getUserInput() (string, string, string, uint) {
var firstName string
var lastName string
var email string
var userTickets uint
// Ask the user for their data
fmt.Println("Enter your first name:")
fmt.Scan(&firstName)
fmt.Println("Enter your last name:")
fmt.Scan(&lastName)
fmt.Println("Enter your email address:")
fmt.Scan(&email)
fmt.Println("Enter the number of tickets you want to book:")
fmt.Scan(&userTickets)
return firstName, lastName, email, userTickets
}
// Rest of the code...
The getUserInput()
function prompts the user to enter their details and returns the provided values.
Validating User Input
Before booking the tickets, it's essential to validate the user input. We'll create a separate file called helper.go
to handle the validation logic. Here's the code:
// helper.go
package main
import "strings"
func validateUserInput(firstName string, lastName string, email string, userTickets uint, remainingTickets uint) (bool, bool, bool) {
isValidName := len(firstName) >= 2 && len(lastName) >= 2
isValidEmail := strings.Contains(email, "@")
isValidTicketNumbers := userTickets > 0 && userTickets <= remainingTickets
return isValidName, isValidEmail, isValidTicketNumbers
}
The validateUserInput()
function takes user input as arguments and performs basic validation checks on the input values.
Booking Tickets
Once the user input is validated, we can proceed to book the tickets. The bookTicket()
function will handle this task:
// main.go
// Rest of the code...
func bookTicket(userTickets uint, firstName string, lastName string, email string) {
remainingTickets -= userTickets
bookings = append(bookings, firstName+" "+lastName)
fmt.Printf("Thank you, %v %v, for booking %v tickets. You will receive a confirmation email at %v\n", firstName, lastName, userTickets, email)
fmt.Printf("%v tickets are remaining for %v\n", remainingTickets, ConferenceName)
}
// Rest of the code...
In the code snippet above, we subtract the number of tickets booked from the remainingTickets
variable and add the user's name to the bookings
slice.
Putting It All Together
Now that we have defined all the necessary functions, let's bring everything together in the main()
function:
// main.go
// Rest of the code...
func main() {
greetUsers()
for {
firstName, lastName, email, userTickets := getUserInput()
isValidName, isValidEmail, isValidTicketNumbers := validateUserInput(firstName, lastName, email, userTickets, remainingTickets)
if isValidName && isValidEmail && isValidTicketNumbers {
bookTicket(userTickets, firstName, lastName, email)
firstName := getFirstName()
fmt.Printf("The first names of bookings are: %v\n", firstName)
if remainingTickets == 0 {
fmt.Println("Our conference is booked out")
break
}
} else {
if !isValidName {
fmt.Println("The first name or last name you entered is too short")
}
if !isValidEmail {
fmt.Println("The email address you entered is invalid")
}
if !isValidTicketNumbers {
fmt.Println("The number of tickets you entered is invalid")
}
fmt.Println("Your input data is invalid. Please try again.")
}
}
}
The main()
function presents a loop that repeatedly asks for user input, validates it, and books the tickets if the input is valid. It also checks if all the tickets have been booked and terminates the program if that's the case.
Conclusion
Congratulations! You have successfully built a basic ticket booking app using Golang. In this tutorial, we covered gathering user input, validating the input, and booking tickets based on the user's request. You can further enhance this app by adding features such as payment processing, seat selection, and user authentication.
Feel free to explore the complete code on GitHub and modify it according to your needs. If you have any questions or feedback, please leave a comment below.
Happy coding! ๐ปโจ
Connect with Me! ๐
If you enjoyed building this ticket-booking app and want to explore more exciting projects and tutorials, feel free to connect with me on the following platforms:
๐ Hashnode
๐ฆ Twitter
๐ผ LinkedIn
๐ป GitHub
๐animesh
Thank you