📝 Program: main.go
package main
import "fmt"
// 📘 Main function is the entry point of the program.
func main() {
// 🎯 Declaring an integer variable 'x' and assigning it a value of 5
var x int = 5
// 💡 Printing the value of x
fmt.Println("📌 The value of variable x is:", x)
}
📖 Explanation
Line | Code | Description |
---|---|---|
1 | package main |
Declares that this file belongs to the main package (entry point). |
3 | import "fmt" |
Imports the fmt package for formatted I/O. |
6 | func main() { |
Declares the main function, which Go looks for to run the program. |
8 | var x int = 5 |
Declares x as an int and initializes it with the value 5 . |
11 | fmt.Println(...) |
Prints the value of x to the console. |
🚀 How to Run This Program
💻 Prerequisites
- Install Go from https://go.dev/dl
🛠️ Steps to Execute
- Create a file named
main.go
:touch main.go
- Paste the code above into
main.go
. - Open terminal and navigate to the folder where
main.go
is saved. - Run the program:
go run main.go
- ✅ You’ll see the output:
📌 The value of variable x is: 5
🎨 Output (Styled)
🌈 Golang Variable Declaration Demo 🌈
-------------------------------------
📌 The value of variable x is: 5
🧠 Key Takeaways
var x int = 5
shows explicit declaration with type and initialization.- You could also write it as
x := 5
(type inferred) — but here we showcase the explicitvar
syntax.
Would you like a similar program with user input or a web interface as well?