Hey there! Now that you're excited about building our ticket booking system, let's get your development environment ready. Don't worry - I'll walk you through every single step!
Setting Up Your Development Environment
Step 1: Installing Go ๐ ๏ธ
First things first - let's get Go installed on your machine. This is like setting up your workshop before starting to build something cool!
Download Go here: https://go.dev/dl/
After installation, open your terminal and type:
go version
If you see something like go version go1.23.2
, give yourself a high five! You're ready to go! ๐
How to Check Go Version
Hey there! Let me show you exactly how to check your Go version on both Windows and Mac. I'll guide you through different ways to open the terminal and run the command. ๐ฅ๏ธ
Method 1: Using Command Prompt/Terminal
Open Command Line Interface:
Windows: Press
Windows + R
, typecmd
, press Enter(Mac: Press
Cmd + Space
, typeterminal
, press Enter)
OR
Windows: Click the Windows Start button, type
cmd
, click "Command Prompt" app(Mac: Click Launchpad, type
terminal
, click Terminal app)
Check Go Version:
In the window that opens, type:
go version
Press Enter
Method 2: Using PowerShell/Terminal Alternative
Open Alternative Shell:
Windows: Right-click on Windows Start button, select "Windows PowerShell" or "Windows Terminal"
(Mac: Open Spotlight with
Cmd + Space
, typeterminal
, press Enter)
OR
Windows: Press
Windows + X
, click "Windows PowerShell" or "Windows Terminal"(Mac: Find Terminal in Applications > Utilities > Terminal)
Check Go Version:
Type:
go version
Press Enter
What You Should See
If Go is installed correctly, you'll see something like:
Windows:
go version go1.23.2 windows/amd64
(Mac:
go version go1.23.2 darwin/amd64
)
Step 2: Installing VS Code ๐ ๏ธ
First, let's get VS Code installed:
Head over to https://code.visualstudio.com/
Download the version for your operating system
Run the installer
Simple as that! But wait - VS Code by itself is like a workshop without tools. Let's add the right tools to make Go development awesome!
Essential Extensions for Go ๐ง
Open VS Code, and let's install some super helpful extensions. These are like power tools for your coding workshop!
1. Go Extension (Most Important!)
Press
Ctrl+Shift+X
(Windows/Linux) orCmd+Shift+X
(Mac) to open the Extensions viewSearch for "Go" (by "Go Team at Google")
Click Install
After installing, VS Code might show a popup saying "Analysis Tools Missing" - click "Install All" when you see this. This installs additional tools that will help you:
Auto-complete code
Format your code automatically
Find and fix errors
And much more!
2. Other Helpful Extensions
Let's add some more awesome tools:
Code Runner: Run your code with a single click
Error Lens: See errors and warnings inline while you code
GitLens: Super helpful if you're using Git
To install these:
Search for each one in the Extensions marketplace
Click Install
Reload VS Code when prompted
Remember: A well-configured editor is like a well-organized kitchen - it makes cooking (or in our case, coding) much more enjoyable!
Step 3: Setting Up Your Project ๐
Let's create a home for our project. Open your terminal and run:
# Create a new directory mkdir ticket-booking cd ticket-booking # Initialize Go module go mod init ticket-booking
Think of go mod init as telling Go "Hey, I'm starting a new project here!" It creates a special file called go.mod that helps manage our project.
Understanding Directory Commands
Hey there! Let's break down these commands that help us create and navigate our project folder. Think of this like creating and entering your project's workspace! ๐
Command 1: mkdir ticket-booking
What does mkdir
mean?
mkdir
= "make directory"It's like telling your computer "Hey, create a new folder for me!"
How to use it:
1. Using Command Line:
Windows: mkdir ticket-booking
OR md ticket-booking
(Mac: mkdir ticket-booking
)
2. Using GUI Method:
Windows: Right-click โ "New" โ "Folder" โ Type "ticket-booking"
(Mac: Right-click โ "New Folder" โ Type "ticket-booking")
What happens after running this command?
A new folder named "ticket-booking" is created
You can find it in your current location
It's empty and ready for your project files
Command 2: cd ticket-booking
What does cd
mean?
cd
= "change directory"It's like telling your computer "I want to go into this folder"
How to use it:
cd ticket-booking
(Same command for both Windows and Mac)
What happens after running this command?
You are now working inside the ticket-booking folder
Any new files you create will be inside this folder
Checking Your Location:
Windows: Type
cd
to show current path(Mac: Type
pwd
to show current path)
Additional Useful Commands:
Windows:
dir
(list files in current directory)(Mac:
ls
(list files in current directory))
Real World Analogy ๐
Think of it like this:
mkdir
is like building a new room in your housecd
is like walking into that roomYour terminal is like your personal assistant that helps you move around
Understanding Go Modules: The go mod init
Command
Hey there! Let's talk about one of the most important commands when starting a new Go project: go mod init
. Think of this as setting up the foundation for your Go project! ๐๏ธ
What is โgo mod initโ
? ๐ค
Simple Explanation
Imagine you're starting a new project - like building a house. Before you can start building, you need:
A blueprint (your project structure)
A list of materials (your dependencies)
An address (your module name)
go mod init
creates this setup for you! It's like getting your building permit before construction.
Technical Explanation
The command go mod init ticket-booking
does two main things:
Creates a new file called
go.mod
Establishes your project as a Go module named "ticket-booking"
The Command Structure
go mod init ticket-booking
Breaking it down:
go
: The Go command-line toolmod
: We're working with modulesinit
: We're initializing a new moduleticket-booking
: The name of our module
What Does It Create? ๐
After running the command, you'll get a go.mod
file that looks like this:
go: creating new go.mod: module ticket-booking
This is like your project's ID card, containing:
Module name
Go version
(Later) Any dependencies you add
Why Do We Need This? ๐ฏ
1. Dependency Management
Tracks what external packages you use
Ensures everyone uses the same versions
Makes your project reproducible
2. Module Identity
Gives your project a unique name
Helps Go understand how to import your code
Makes your code shareable
3. Version Control
Helps manage different versions of dependencies
Makes sure your project works consistently
Common Naming Conventions ๐
1. For Local Development:
go mod init project-name
2. For GitHub Projects:
go mod init github.com/username/project-name
3. For Company Projects:
go mod init company.com/project-name
Project Structure ๐๏ธ
Now, let me show you how we'll organize our code. Good organization is like having a clean workspace - it makes everything easier!
ticket-booking/ |__ main.go # Our main program file |__ go.mod # Project configuration |__ README.md # Project documentation
Let's create our first file: main.go
Creating Your First Go File
Let's create our main Go file! I'll show you multiple ways to do this on both Windows and Mac. Choose the method that feels most comfortable for you!
Method 1: Using VS Code (Recommended) ๐จโ๐ป
1. Open VS Code:
Windows: Double click VS Code icon
(Mac: Double click VS Code in Applications)
2. Create new file:
Windows: Click the "New File" icon OR press
Ctrl + N
(Mac: Click the "New File" icon OR press
Cmd + N
)
OR
Windows: Go to File > New File
(Mac: Go to File > New File)
3. Save as main.go:
Windows: Press
Ctrl + S
, typemain.go
(Mac: Press
Cmd + S
, typemain.go
)Click Save
Method 2: Using Command Line ๐ป
Command Prompt (Windows):
echo. > main.go
OR
type nul > main.go
Terminal (Mac):
touch main.go
Method 3: Using PowerShell/Terminal Alternative ๐ฅ๏ธ
PowerShell (Windows):
New-Item -Name "main.go" -ItemType "file"
OR shorter version:
ni main.go
Alternative Terminal Command (Mac):
echo "" > main.go
Verifying File Creation ๐
Check if file exists:
Windows:
dir main.go
(Mac:
ls main.go
)
View file contents:
Windows:
type main.go
(Mac:
cat main.go
)
Project Structure So Far ๐
Your project should now look like this:
ticket-booking/ |__ go.mod # Created by go mod init |__ main.go # Our new empty file
Now that we have our main.go file, we're ready to:
Add our first Go code
Learn about Go packages
Write our ticket booking system
Would you like to continue with adding our first Go code?
Writing Our First Lines of Code ๐จโ๐ป
Open main.go and let's write our first piece of Go code:
package main import "fmt" func main() { fmt.Println("Welcome to Triangle.Technology English Course Booking System!") fmt.Println("We have 30 total tickets available for the course") }
Let's break this down - it's simpler than it looks!
package main
- This is like telling Go "This is where my program starts"import "fmt"
- We're bringing in Go's formatting package (think of it as our tool for printing text)func main()
- This is the entry point of our program, like the front door of a house
Understanding Go's Basic Program Structure
Hey there! Let's dive deeper into these three fundamental lines of code that every Go program needs. Think of them as the blueprint for your program! ๐๏ธ
1. package main
package main
Think of packages in Go like departments in a big company:
Every Go file must declare which package it belongs to
main
is a special package name - it tells Go "This is an executable program"If you're building an application (not a library), your primary file MUST be in
package main
Real-world analogy:
Imagine you're organizing a big office building:
Each department (package) has its specific role
The main lobby (package main) is where everyone enters the building
Without a main lobby, people can't enter the building (program won't run)
2. import "fmt"
import "fmt"
This is like bringing tools into your workshop:
import
tells Go "I need to use these tools"fmt
is Go's formatting package (short for "format")It's like your program's communication toolkit
What can fmt do?
// Print simple text fmt.Println("Hello!") // Prints: Hello! // Print formatted text fmt.Printf("I am %v years old", 25) // Prints: I am 25 years old // Get user input fmt.Scan(&userInput) // Reads what user types
Think of fmt
as your program's voice and ears - it helps you talk to users and listen to their responses! ๐ค๐
1. fmt.Println()
- The Simple Printer
fmt.Println(โHello!โ)
Think of Println
as your program's way of speaking in complete sentences:
It prints the text and automatically adds a new line at the end
Like pressing Enter after typing a message
2. fmt.Printf()
- The Formatted Printer
fmt.Printf(โI am %v years oldโ, 25)
Printf
is like having a template for your message where you can fill in the blanks. In this example, %v
is a placeholder that gets replaced with the value 25
. It's like saying:
"I am ___ years old" (where the blank space is
%v
)Then Go fills in that blank with
25
So it prints: "I am 25 years old"
Pro Tip ๐ก: %v
is the most flexible placeholder - it works with any type of value. However, there are more specific placeholders like %s
for strings, %d
for integers, and %.2f
for floating-point numbers with 2 decimal places. We'll learn more about formatting with strings, integers and floating-point numbers in later sections. ๐
Since we know 25 is a number, we could also use %d
instead of %v
:
fmt.Printf(โI am %d years oldโ, 25)
Both will work!
3. fmt.Scan()
- The Listener
var userInput string
fmt.Scan()
Scan
is like your program's ears - it waits for the user to type something.
Real-world analogy:
If you're building furniture, you need to bring your tools first
import
is like going to your tool shedfmt
is like grabbing your communication tools (megaphone, notepad, etc.)
3. func main()
func main() { // Your code goes here }
This is the heart of your program:
func
tells Go "I'm creating a function"main
is a special name that Go looks forEverything inside the curly braces
{}
is what your program will do
Why is main() special?
It's the starting point of your program
Go automatically runs
main()
firstWithout
main()
, Go wouldn't know where to begin
Real-world analogy: Imagine directing a play:
The script (program) has many scenes (functions)
But you need to know which scene comes first (main)
main()
is like Act 1, Scene 1 of your play
Putting It All Together
package main // 1. "This is an executable program" import "fmt" // 2. "I need communication tools" func main() { // 3. "Start here" fmt.Println("Hello, Triangle.Technology") // Use those tools }
Think of it like preparing for a presentation:
You enter the right building (package main)
Grab your presentation tools (import "fmt")
Start your presentation (func main)
Common Questions ๐ค
Q: Why do we need package main?
A: Go needs to know if your code is:
An executable program (package main)
Or a library for other programs to use (different package name)
Q: Can we skip importing fmt?
A: Yes, but only if you don't need to:
Print anything to the screen
Get input from the user
Q: Can we have multiple main() functions?
A: No! Just like a building can't have two main entrances, your program can only have one starting point.
Pro Tips ๐ก
1. Package Organization:
// Good: Clear and organized package main import "fmt" func main() {} // Bad: Hard to read package main; import "fmt"; func main() {}
2. Import Multiple Packages:
// For multiple imports, use this format import ( "fmt" "strings" )
3. Main Function Clarity:
func main() { // Start with initialization fmt.Println("Starting program...") // Then your main logic // Then cleanup if needed }
Remember:
This structure is like the foundation of a house
Every Go program you write will start with these elements
Understanding them well will make learning Go much easier!
Ready to build more on this foundation? Let's keep going! ๐
Try It Out! ๐
Let's run our program! In your terminal:
go run main.go
You should see:
Welcome to Triangle.Technology English Course Booking System!
We have 30 total tickets available for the course
Amazing! You've just written and run your first Go program! ๐
Different Ways to Open Terminal
Opening the Integrated Terminal in VS Code:
Method 1: Keyboard Shortcuts (Fastest Way!)
On Windows/Linux: Press
Ctrl + `
(that's the backtick key, usually under the Esc key)On Mac: Press
Cmd + `
Method 2: Using the Menu
Click on "Terminal" in the top menu
Select "New Terminal"
Method 3: Using the Command Palette
Press
Ctrl+Shift+P
(Windows/Linux) orCmd+Shift+P
(Mac)Type "terminal"
Select "Terminal: Create New Terminal"
Pro Tips for Terminal Usage ๐ก
Multiple Terminals: You can have several terminals open at once
Click the
+
icon in the terminal panel to add moreUse the dropdown to switch between them
Split Terminal: Want to see two terminals side by side?
Click the "Split Terminal" icon (looks like a rectangle split in two)
Or right-click in the terminal and select "Split Terminal"
Terminal Location: The terminal automatically opens in your project folder
Verify this by typing
pwd
(Print Working Directory)You should see your ticket-booking project path
Terminal Customization:
Change the default shell: Click the dropdown arrow in the terminal
Adjust font size:
Ctrl +
orCtrl -
(Windows/Linux),Cmd +
orCmd -
(Mac)
Let's Add Some Variables
Now, let's make our program more flexible by adding some variables:
package main import "fmt" const conferenceName = "Triangle.Technology English Course" const conferenceTickets = 30 var remainingTickets uint = 30 func main() { fmt.Printf("Welcome to %v Booking System!\n", conferenceName) fmt.Printf("We have total of %v tickets and %v are still available.\n", conferenceTickets, remainingTickets) fmt.Println("Get your tickets here to attend") }
Some cool things to notice here:
const
is for values that won't change (like the total number of tickets)var
is for values that will change (like remaining tickets)%v
in Printf is like a placeholder where Go will put our values\
Understanding Constants, Variables, and Formatting in Go
Hey there! Let's break down these fundamental concepts in Go. I'll explain them in a way that'll make perfect sense! ๐ฏ
1. Constants (const
)
You know how some things in life should never change? Like the number of days in a week or the rules of a game? Well, that's exactly what constants are in Go! They're values that, once set, stay the same forever.
// Declaring constants const maxTickets = 30 const courseName = "English Speaking Course" const isPremium = true
Why Do We Use Constants? ๐ค
Let me show you with some real examples:
1. They Protect Us From Accidental Changes:
const maxTickets = 30 maxTickets = 40 // Error! Can't change a constant
Think of it like setting the maximum capacity of a classroom. Once you've decided it's 30 students, you don't want someone accidentally changing that number!
2. They Make Our Intentions Clear:
// Using a constant shows this should never change const taxRate = 0.1 //Using a variable might suggest this could change var currentPrice = 99.99
2. Variables (var
)
Think of a variable like a labeled box where you can store stuff. Just like how you might have different boxes for different things in your house:
A box for shoes (numbers)
A box for books (text)
A box for toys (different types of items)
In Go, these boxes (variables) can store different types of data, and you can change what's inside them whenever you need to!
// Method 1: Using var with explicit type var studentCount int = 0 // Method 2: Using var with type inference var courseName = "English Course" // Method 3: Short declaration (most common inside functions) tickets := 25
Why Use Variables? ๐
1. For Values That Change:
You know how in our ticket booking system, the number of available tickets keeps changing? That's exactly why we need variables! Let me show you:
var remainingTickets uint = 30 // When someone books 2 tickets remainingTickets = remainingTickets -2 // Now it's 28
2. For Storing User Input:
Variables are perfect for storing information that users type into your program. Check this out:
var userName string fmt.Scan(&userName) // User can input different names
3. For Calculations:
This is where variables really shine! They help us do math and calculations in our programs:
var totalTickets uint = 30 var ticketPrice float64 = 25.50 var studentDiscount float64 =5.00 //Basic calculations var totalRevenue float64 = float64(totalTickets) * ticketPrice var studentPrice = ticketPrice - studentDiscount
Pro Tip ๐ก:
Use uint for simple positive counts
Use int when you might need negative numbers
Use uint64/int64 for very large numbers
Use float64 for accurate decimal calculations. When in doubt with money calculations, float64 is your safest bet! ๐ช
Use string type when the numbers are just for display or identification and won't be used in calculations (like phone numbers, postal codes, ID numbers).
Remember! Variables are like your program's memory - they help you keep track of things that change while your program is running. The more you practice using them, the more natural they'll feel!
Cool Tips About Variables! ๐ก
1. Short Declaration (The Quick Way)
Instead of writing var
, you can use :=
for a shortcut:
// Instead of: var name string = "John" // You can write: name := "John" // Go figures out the type automatically!
2. Multiple Variables at Once
// Declare multiple variables in one line firstName, lastName := "John", "Doe" //Or multiple variables of the same type var apple, banana, orange int = 5, 3, 4
Real-world Booking System Example:
const maxTickets = 30 // This never changes var remainingTickets = 30 // This will decrease as tickets are sold var bookingCount = 0 // This will increase with each booking func bookTicket() { remainingTickets-- // Can change bookingCount++ // Can change // maxTickets-- // Error! Can't change a constant }
3. Format Verbs (Those % Things)
Hey there! You know those %v
symbols we saw earlier? Let's talk about them! I know they might look a bit weird at first - trust me, I felt the same way when I first learned Go. But once you get the hang of them, they're actually pretty cool!
Think of format verbs (yeah, that's what Go calls them!) like little magic spells that tell Go how to present our values. It's kind of like having different types of containers for different things - you wouldn't drink soup from a fork, right? ๐
Let's Look at Our Format Verb Family!
// Let's create some variables to play with name := "John" age := 25 price := 29.99 isStudent := true
Now Let's see these format verbs in action!
// %v - The "I'll handle anything" verb fmt.Printf("Value: %v\n", name) // Value: John fmt.Printf("Value: %v\n", age) // Value: 25 fmt.Printf("Value: %v\n", price) // Value: 29.99 fmt.Printf("Value: %v\n", isStudent) // Value: true // %s - Just for strings fmt.Printf("Name: %s\n", name) // Name: John // %d - For integers (whole numbers) fmt.Printf("Age: %d years old\n", age) // Age: 25 years old // %f - For floating-point numbers (decimals) fmt.Printf("Price: $%f\n", price) // Price: $29.990000 fmt.Printf("Price: $%.2f\n", price) // Price: $29.99 // %t - For booleans (true/false) fmt.Printf("Student: %t\n", isStudent) // Student: true
Some Cool Party Tricks! ๐ฉ
You know what's really fun? We can do some neat formatting tricks with these verbs. Check this out:
// Want to make things line up nicely? Try this: fmt.Printf("Name: %10s\n", name) // Name: John (right-aligned) fmt.Printf("Name: %-10s\n", name) // Name: John (left-aligned) // This is super helpful for money - nobody likes seeing $29.990000000! fmt.Printf("Price: $%.2f\n", price) // Price: $29.99 fmt.Printf("Price: $%.1f\n", price) // Price: $30.0 //And here's my favorite - combining multiple values in one line! fmt.Printf("Hi, I'm %s and I'm %d years old!\n", name, age) // Hi, I'm John and I'm 25 years old!
Real-World Examples You'll Actually Use ๐ฏ
Let's use these in our ticket booking system - this is where it gets really practical!
// Let's set up our ticket sale tickets := 5 cost := 25.50 // When showing the price to customers fmt.Printf("Ticket cost: $%.2f\n", cost) // Ticket cost: $25.50 // Creating a nice order summary fmt.Printf("Order summary: %d tickets at $%.2f each\n", tickets, cost) // Order summary: 5 tickets at $25.50 each //Showing the final total total := float64(tickets) * cost fmt.Printf("Total cost: $%.2f\n", total) // Total cost: $127.50
Here's a little secret I wish someone had told me when I started: ๐ก
When in doubt, use
%v
- it's like that friend who gets along with everyone! It'll handle pretty much any type of value you throw at it.For money stuff,
%.2f
is your best friend - keeps those cents in check!For names and text, stick with
%s
- it's made for the jobCounting things? That's what
%d
lives for!
You know what's really cool about these format verbs? They're like having a personal stylist for your data! They make sure everything looks exactly the way you want it to when it shows up on the screen.
I know it might seem like a lot to remember at first, but trust me - after you use these a few times, they'll become second nature. It's just like learning any new language - practice makes perfect!
Quick Challenge! ๐ช
Now that we understand how variables and constants work in Go, let's see them in action in our booking system!
Try running this new code. Notice how we're using our knowledge about:
Constants for fixed values (our total ticket capacity won't change)
Variables for values that need to change (remaining tickets will decrease as people book)
Printf with %v for clean formatting
The cool part? We can now easily adjust our program! Try these exercises:
Change the course name to "Triangle.Technology Advanced English"
Update the total tickets to 40
Run the program again to see your changes
See how making these changes is much easier with variables? This is why we structure our code this way - it makes our program flexible and easy to maintain!
Now that we've got our foundation solid with variables and basic output, shall we move on to making our program interactive? We'll need to let users input their booking details! ๐ฏ
But before we move on - any questions about what we've covered so far? Remember, a solid foundation is crucial for building something awesome!
In our next section, we'll start adding user input functionality. Get ready to make your program interactive! Are you excited? I know I am! ๐
-
Keep your terminal open - we'll be using it a lot
If you make a mistake, don't worry! Error messages in Go are actually very helpful
Try playing around with the values in the variables and see how the output changes
Consider using VS Code with the Go extension for a better coding experience