VS Code
VS Code (Visual Studio Code) is a lightweight and powerful code editor used for writing and managing code.
What I Learned:
- How to create a new Go file (main.go)
- Installing Go extensions
- Running Go programs using the terminal:
go run main.go
package
In Go, every file belongs to a package.
package main
package main is required for executable programs.
It tells Go that this file will run as an application.
import
The import keyword allows us to use packages from Go's standard library.
import "fmt"
"fmt" is used for formatted input and output.
Without importing it, we cannot use functions like fmt.Println() or fmt.Scan().
func main ()
The main function is the entry point of a Go program.
func main() {
fmt.Println("Hello, World!")
}
The program starts executing inside func main().
var (Variables)
Variables store data.
var name string = "Kervie"
var age int = 23
Can also shortened by : before the =;
name := "Kervie"
age := 23
const (Constant)
Constant store values that Cannot be changed.
const pi = 3.14
Once declared, it cannot be reassigned.
fmt.Println()
Used to print output to the console.
fmt.Println("Hello!")
Automatically adds a newline(ln) at the end.
%v (Format Verb)
%v is used inside formatted printing functions.
fmt.Printf("My name is %v", name)
%v prints the value in its default format.
Other examples:
- %d - integer
- %s - string
- %f - float
Pointers
var x int = 10
var p *int = &x
&x - gets the address of x
*p - gets the value stored at that address
Example:
fmt.Println(x) // 10
fmt.Println(p) // memory address
fmt.Println(*p) // 10
User Input
Go allows user input using the fmt package.
fmt.Scan()
Used to read input from the user.
var name string
fmt.Scan(&name)
The & is important because fmt.Scan() needs the memory address of the variable.
It stores the user input inside the variable.
Example:
package main
import "fmt"
func main() {
var name string
fmt.Println("Enter your name:")
fmt.Scan(&name)
fmt.Println("Hello,", name)
}
Example For Better Understanding
A simple Go Conference Booking App.
Program Stucture
package main
package main
import "fmt"
import "fmt"
func main()
func main () {
}
Variables (var)
var conferenceName = "Go Conference"
var remainingTickets = 50
-
varis used to declare variables. - Variables can change during program execution.
- Go automatically detects the type if not specified.
Example with explicit type:
var fName string
var lName string
var email string
var userTickets int
Constants (const)
const conferenceTickets = 50
-
constis used for values that never change. - Once declared, it cannot be modified.
Printing Output
fmt.Printf()
Used for formatted output.
fmt.Printf("conferenceName is %T\n", conferenceName)
Format verbs learned:
| Verb | Meaning |
|---|---|
%T |
Type of variable |
%v |
Default value format |
\n |
New line |
Example:
fmt.Printf("Welcome to our %v booking application!\n", conferenceName)
fmt.println()
fmt.Println("Get your tickets here to attend")
- Prints text
- Automatically adds a new line
fmt.Print()
fmt.Print("Enter your first name: ")
- Prints text
- Does NOT automatically add a new line
User Input
fmt.Scan()
fmt.Scan(&fName)
- The
&symbol passes the memory address of the variable. -
fmt.Scan()stores the user input into that memory location.
Example:
var fName string
fmt.Scan(&fName)
Pointers (Related to &)
When using:
fmt.Scan(&fName)
-
&fNamemeans: "give me the address of fName" - This allows the function to modify the variable.
Updating Variables
remainingTickets = remainingTickets - userTickets
- This updates the value stored in
remainingTickets. - Since it is declared with
var, it can change.
Full Example Logic Simplified
remainingTickets = remainingTickets - userTickets
fmt.Printf("Thank you %v %v for booking %v tickets.\n", fName, lName, userTickets)
fmt.Printf("%v remaining tickets for %v", remainingTickets, conferenceName)
Full source code for better understanding:
package main
import "fmt"
func main() {
var conferenceName = "Go Conference"
const conferenceTickets = 50
var remainingTickets = 50
fmt.Printf("conferenceName is %T, conferenceTickets is %T, remainingTickets is %T\n", conferenceName, conferenceTickets, remainingTickets)
fmt.Printf("Welcome to our %v booking application!\n", conferenceName)
fmt.Printf("We have %v tickets and %v are vailable\n", conferenceTickets, remainingTickets)
fmt.Println("Get your tickets here to attend")
var fName string
var lName string
var email string
var userTickets int
// asking for user details
fmt.Print("Enter you first name: ")
fmt.Scan(&fName)
fmt.Print("Enter you lasr name: ")
fmt.Scan(&lName)
fmt.Print("Enter you email address: ")
fmt.Scan(&email)
fmt.Print("How many ticket you get: ")
fmt.Scan(&userTickets)
remainingTickets = remainingTickets - userTickets
fmt.Printf("Thank you %v %v for booking %v tickets. You will recieve a confirmation email at %v\n", fName, lName, userTickets, email)
fmt.Printf("%v remaining tikcets for %v", remainingTickets, conferenceName)
}
Today I learned how to structure a Go program using package main, import, and func main() as the entry point. I practiced declaring variables with var, creating constants with const, and understanding data types using format verbs like %T and %v. I used fmt.Print, fmt.Println and fmt.Printf to display formatted output in the console. I also learned how to collect user input with fmt.Scan() and why pointers (&) are needed to store values in variables.
Top comments (0)